PHP實現(xiàn)基于面向?qū)ο蟮膍ysqli擴展庫增刪改查操作工具類
本文實例講述了PHP實現(xiàn)基于面向?qū)ο蟮膍ysqli擴展庫增刪改查操作工具類。分享給大家供大家參考,具體如下:
mysqli擴展庫是MySQL擴展庫的改進(jìn)版本,在mysql擴展庫的基礎(chǔ)上提高了穩(wěn)定性和效率,mysqli擴展庫有兩套東西,一套就是面向過程的mysqli另一套是面向?qū)ο蟮膍ysqli。操作方式大體和mysql擴展庫大體一致,這次還是先抽取出來一個操作mysql的工具類,和調(diào)用的類。
1. mysqli擴展庫操作數(shù)據(jù)庫工具類
<?php //數(shù)據(jù)庫操作類 class DBUtil{ private $host="localhost"; private $username="root"; private $password="123456"; private $dbname="student"; private $conn; public function DBUtil(){ $this->conn=new mysqli($this->host, $this->username, $this->password,$this->dbname) or die($this->conn->connect_error); } //查詢 public function query($sql){ $all= $this->conn->query($sql); return $all; } //插入,修改,刪除 public function otherOperate($sql){ if($this->conn->query($sql)){ if($this->conn->affected_rows>0){ return "OK"; }else{ return "ERROOR"; } } } public function close(){ $this->conn->close(); } } ?>
2. 下面是具體的調(diào)用工具類的代碼
<?php require_once "MySQLUtil.php"; /*$sql="select * from m_student"; $util=new DBUtil(); $result=$util->query($sql); while($row=$result->fetch_assoc()){ echo "$row[stuName]"."</br>"; } $result->free(); $util->close();*/ $sql="update m_student set stuName='楊冪' where id=3"; $util=new DBUtil(); $result=$util->otherOperate($sql); echo $result; $util->close(); ?>
如果要用到其他方法可以查閱php開發(fā)文檔。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php 表單提交大量數(shù)據(jù)發(fā)生丟失的解決方法
這篇文章主要介紹了php表單提交大量數(shù)據(jù)、上千個文本框時發(fā)生數(shù)據(jù)丟失、數(shù)據(jù)不完整等問題的解決方法,需要的朋友可以參考下2014-03-03php的crc32函數(shù)使用時需要注意的問題(不然就是坑)
這篇文章主要介紹了php的crc32函數(shù)使用時需要注意的問題(不然就是坑) ,需要的朋友可以參考下2015-04-04PHP自定義函數(shù)獲取搜索引擎來源關(guān)鍵字的方法
這篇文章主要介紹了PHP自定義函數(shù)獲取搜索引擎來源關(guān)鍵字的方法,涉及php針對來路頁面URL的分析與判斷技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-07-07