This article mainly introduces the PHP Model base class based on mysqli, and provides the complete implementation and usage of the database base class, for more information about how to implement the mysqli-based Model class in PHP, see the example in this article. We will share this with you for your reference. The details are as follows:
DB. class. php
<? Php // database connection class DB {// Obtain the object handle static public function getDB () {$ _ mysqli = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME ); if (mysqli_connect_errno () {echo 'database connection error! Error Code :'. mysqli_connect_error (); exit () ;}$ _ mysqli-> set_charset ('utf8'); return $ _ mysqli ;}// clear, release the static public function unDB (& $ _ result, & $ _ db) {if (is_object ($ _ result) {$ _ result-> free (); $ _ result = null;} if (is_object ($ _ db) {$ _ db-> close (); $ _ db = null ;}}?>
Model. class. php
<? Php // Model base class Model {// execute multiple SQL statements public function multi ($ _ SQL) {$ _ db = DB: getDB (); $ _ db-> multi_query ($ _ SQL); DB: unDB ($ _ result = null, $ _ db); return true ;} // Obtain the next value-added id model public function nextid ($ _ table) {$ _ SQL = "SHOW TABLE STATUS LIKE '$ _ table '"; $ _ object = $ this-> one ($ _ SQL); return $ _ object-> Auto_increment;} // query the total record Model protected function total ($ _ SQL) {$ _ db = DB: getDB (); $ _ result = $ _ db-> q Uery ($ _ SQL); $ _ total = $ _ result-> fetch_row (); DB: unDB ($ _ result, $ _ db ); return $ _ total [0] ;}// find a single data model protected function one ($ _ SQL) {$ _ db = DB: getDB (); $ _ result = $ _ db-> query ($ _ SQL); $ _ objects = $ _ result-> fetch_object (); DB: unDB ($ _ result, $ _ db); return Tool: htmlString ($ _ objects);} // search for multiple data models 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);} // added, deleted, and repaired model protected function aud ($ _ SQL) {$ _ db = DB: getDB (); $ _ db-> query ($ _ SQL); $ _ affected_rows =$ _ db-> affected_rows; DB: unDB ($ _ result = null, $ _ db ); return $ _ affected_rows ;}}?>