PHP implements a complete Model base class instance based on mysqli

Source: Internet
Author: User
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 ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.