Home > Article > Backend Development > PHP uses object-oriented thinking to complete the addition, deletion, modification and query operations of the mysqli extension library.
This article mainly introduces the PHP implementation of the object-oriented mysqli extension library add, delete, modify and query operation tool class, and analyzes the encapsulation and usage skills of the mysqli add, delete, modify and query operation class in the form of examples. Friends in need can refer to the following
The example of this article describes the PHP implementation of the object-oriented mysqli extension library addition, deletion, modification and query operation tool class. Share it with everyone for your reference, the details are as follows:
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 One set 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.
The above is the detailed content of PHP uses object-oriented thinking to complete the addition, deletion, modification and query operations of the mysqli extension library.. For more information, please follow other related articles on the PHP Chinese website!