Home >Backend Development >PHP Tutorial >PHP implementation method of adding, deleting, modifying and checking operation tools based on mysqli extension library
This article mainly introduces PHP to implement the object-oriented mysqli extension library add, delete, modify and check operation tool class. It analyzes the encapsulation and usage skills of mysqli add, delete, modify and check operation class in the form of examples. Friends who need it can refer to it. I hope it can Help everyone.
The mysqli extension library is an improved version of the MySQL extension library. It improves stability and efficiency based on the mysql extension library. The mysqli extension library has two sets of things, one is process-oriented mysqli and the other is Object-oriented mysqli. The operation method is generally the same as that of the mysql extension library. This time, we first extract a tool class for operating mysql and the calling class.
1. Mysqli extension library operates database tool class
<?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. The following is the specific code for calling the tool class
<?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(); ?>
If you want to use other methods, you can check the PHP development documentation.
Related recommendations:
mysqli batch execution of multiple statements and a function call to execute multiple statements
PHP about mysqli transaction operations Detailed explanation of common methods
php database connectionThe difference and usage between mysql and mysqli
The above is the detailed content of PHP implementation method of adding, deleting, modifying and checking operation tools based on mysqli extension library. For more information, please follow other related articles on the PHP Chinese website!