search
Homephp教程php手册PHP uses the mysqli extension library to implement addition, deletion, modification and query (object-oriented version)

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

<span style="color: #000000;">php
 </span><span style="color: #008000;">//</span><span style="color: #008000;">数据库操作类</span>
  <span style="color: #0000ff;">class</span><span style="color: #000000;"> DBUtil{
   </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$host</span>="localhost"<span style="color: #000000;">;
   </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$username</span>="root"<span style="color: #000000;">;
   </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$password</span>="123456"<span style="color: #000000;">;
   </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$dbname</span>="student"<span style="color: #000000;">;
   </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$conn</span><span style="color: #000000;">;
   </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> DBUtil(){
     </span><span style="color: #800080;">$this</span>->conn=<span style="color: #0000ff;">new</span> mysqli(<span style="color: #800080;">$this</span>->host, <span style="color: #800080;">$this</span>->username, <span style="color: #800080;">$this</span>->password,<span style="color: #800080;">$this</span>->dbname) or <span style="color: #0000ff;">die</span>(<span style="color: #800080;">$this</span>->conn-><span style="color: #000000;">connect_error);
    
   }
  </span><span style="color: #008000;">//</span><span style="color: #008000;">查询</span>
   <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> query(<span style="color: #800080;">$sql</span><span style="color: #000000;">){
     </span><span style="color: #800080;">$all</span>= <span style="color: #800080;">$this</span>->conn->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
     </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$all</span><span style="color: #000000;">;
   }
  </span><span style="color: #008000;">//</span><span style="color: #008000;">插入,修改,删除</span>
   <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> otherOperate(<span style="color: #800080;">$sql</span><span style="color: #000000;">){
      </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->conn->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">)){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->conn->affected_rows>0<span style="color: #000000;">){
           </span><span style="color: #0000ff;">return</span> "OK"<span style="color: #000000;">;
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
           </span><span style="color: #0000ff;">return</span> "ERROOR"<span style="color: #000000;">;
        }
      }
   }
   </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> close(){
     </span><span style="color: #800080;">$this</span>->conn-><span style="color: #000000;">close();
   }
  }
</span>?>

2. The following is the specific code for calling the tool class

<span style="color: #000000;">php
  </span><span style="color: #0000ff;">require_once</span> "MySQLUtil.php"<span style="color: #000000;">;
   </span><span style="color: #008000;">/*</span><span style="color: #008000;">$sql="select * from m_student";
   $util=new DBUtil();
   $result=$util->query($sql);
   while($row=$result->fetch_assoc()){
        echo "$row[stuName]"."";
   }
   $result->free();
   $util->close();</span><span style="color: #008000;">*/</span>
   <span style="color: #800080;">$sql</span>="update m_student set stuName='杨幂' where id=3"<span style="color: #000000;">;
   </span><span style="color: #800080;">$util</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> DBUtil();
   </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$util</span>->otherOperate(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
   </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;
   </span><span style="color: #800080;">$util</span>-><span style="color: #000000;">close();
</span>?>

Reference reading: www.manongjc.com/article/1206.html

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool