Home > Backend Development > PHP Tutorial > PHP implementation method of adding, deleting, modifying and checking operation tools based on mysqli extension library

PHP implementation method of adding, deleting, modifying and checking operation tools based on mysqli extension library

小云云
Release: 2023-03-19 22:14:01
Original
1816 people have browsed it

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();
  }
 }
?>
Copy after login

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();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template