mysqli擴展庫是mysql擴展庫的改進版本,在mysql擴展庫的基礎上提高了穩定性和效率,mysqli擴展庫有兩套東西,一套就是面向過程的mysqli另一套是面向對象的mysqli。操作方式大體和mysql擴展庫大體一致,這次還是先抽取出來一個操作mysql的工具類,和調用的類。
1.mysqli擴展庫操作數據庫工具類
<?php //數據庫操作類 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.下面是具體的調用工具類的代碼
<?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(); ?>
參考閱讀:www.manongjc.com/article/1206.html