Home > Backend Development > PHP Tutorial > PHP implements a complete example of the Model base class based on mysqli, phpmysqlimodel base_PHP tutorial

PHP implements a complete example of the Model base class based on mysqli, phpmysqlimodel base_PHP tutorial

WBOY
Release: 2016-07-12 08:54:26
Original
917 people have browsed it

PHP implements a complete example of the Model base class based on mysqli, phpmysqlimodel base

The example in this article describes the PHP implementation of the Model base class based on mysqli. Share it with everyone for your reference, the details are as follows:

DB.class.php

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

Copy after login

Model.class.php

<&#63;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 '$_table'";
       $_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;
     }
  }
&#63;>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP database operation skills based on pdo", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "PHP basic Grammar introductory tutorial", "php office document operation skills summary (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) Usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Mysql class implemented by PHP based on singleton mode
  • PHP encapsulated connection Mysql class and usage analysis
  • a You can refer to the php Mysql class to learn and get familiar with
  • Twelve common PHP MySql free CMS systems
  • PHP uses the Mysqli class library to achieve perfect paging effect
  • PHP format Method to return float type from MYSQL
  • php implements Mysql simple operation class
  • php simple operation mysql database class
  • PHP implements complete example of generating MYSQL statement class through parameters

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119983.htmlTechArticlePHP implements a complete example of the Model base class based on mysqli. The phpmysqlimodel basic text example describes the PHP implementation of the Model base based on mysqli. kind. Share it with everyone for your reference, the details are as follows: DB.c...
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