Example sharing on how to implement addition, deletion, modification and query of mysqli extension library (based on object-oriented) in PHP

黄舟
Release: 2023-03-14 10:40:01
Original
1456 people have browsed it

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

The above is the detailed content of Example sharing on how to implement addition, deletion, modification and query of mysqli extension library (based on object-oriented) in PHP. 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