Methods and examples of implementing Model base class based on mysqli in PHP

墨辰丷
Release: 2023-03-29 16:14:02
Original
1368 people have browsed it

This article mainly introduces the methods and examples of implementing the Model base class based on mysqli in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The details are as follows:

DB.class.php

<?php
  //数据库连接类
  class DB {
     //获取对象句柄
     static public function getDB() {
       $_mysqli = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
       if (mysqli_connect_errno()) {
          echo &#39;数据库连接错误!错误代码:&#39;.mysqli_connect_error();
          exit();
       }
       $_mysqli->set_charset(&#39;utf8&#39;);
       return $_mysqli;
     }
     //清理,释放资源
     static public function unDB(&$_result, &$_db) {
       if (is_object($_result)) {
          $_result->free();
          $_result = null;
       }
       if (is_object($_db)) {
          $_db->close();
          $_db = null;
       }
     }
  }
?>
Copy after login

Model.class.php

<?php
  //模型基类
  class Model {
     //执行多条SQL语句
     public function multi($_sql) {
       $_db = DB::getDB();
       $_db->multi_query($_sql);
       DB::unDB($_result = null, $_db);
       return true;
     }
     //获取下一个增值id模型
     public function nextid($_table) {
       $_sql = "SHOW TABLE STATUS LIKE &#39;$_table&#39;";
       $_object = $this->one($_sql);
       return $_object->Auto_increment;
     }
     //查找总记录模型
     protected function total($_sql) {
       $_db = DB::getDB();
       $_result = $_db->query($_sql);
       $_total = $_result->fetch_row();
       DB::unDB($_result, $_db);
       return $_total[0];
     }
     //查找单个数据模型
     protected function one($_sql) {
       $_db = DB::getDB();
       $_result = $_db->query($_sql);
       $_objects = $_result->fetch_object();
       DB::unDB($_result, $_db);
       return Tool::htmlString($_objects);
     }
     //查找多个数据模型
    protected function all($_sql) {
       $_db = DB::getDB();
       $_result = $_db->query($_sql);
       $_html = array();
       while (!!$_objects = $_result->fetch_object()) {
          $_html[] = $_objects;
       }
       DB::unDB($_result, $_db);
       return Tool::htmlString($_html);
     }
     //增删修模型
     protected function aud($_sql) {
       $_db = DB::getDB();
       $_db->query($_sql);
       $_affected_rows = $_db->affected_rows;
       DB::unDB($_result = null, $_db);
       return $_affected_rows;
     }
  }
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php Function uses a variable number of parameter methods

Arrays commonly used in PHP Detailed explanation of operation methods

Detailed explanation of several methods for PHP to obtain user access IP address

The above is the detailed content of Methods and examples of implementing Model base class based on mysqli 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