Home  >  Article  >  Backend Development  >  PHP method based on object-oriented mysqli extension library add, delete, modify and check operation tool class

PHP method based on object-oriented mysqli extension library add, delete, modify and check operation tool class

墨辰丷
墨辰丷Original
2018-05-19 11:35:142005browse

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 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 operation 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=&#39;杨幂&#39; where id=3";
  $util=new DBUtil();
  $result=$util->otherOperate($sql);
  echo $result;
  $util->close();
?>

Related recommendations:

php Object-oriented programming exercises: Calculate the perimeter and area of ​​rectangles, triangles, and circles

PHP object-oriented Static delayed binding static::

PHP object-oriented Static delayed binding static::

The above is the detailed content of PHP method based on object-oriented mysqli extension library add, delete, modify and check operation tool class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn