PHP implementation based on MYSQLI model base class complete Instance _php skill

Source: Internet
Author: User
Tags php database php introduction

This article describes the PHP implementation based on the MYSQLI model base class. Share to everyone for your reference, specific as follows:

DB.class.php

<?php
  //Database Connection classes class
  DB {
     //Get 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;
     }
     Cleanup, releasing resources
     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 classes Class Model {//EXECUTE multiple SQL statements public Function multi ($_sql) {$_db = Db::getdb ();
       $_db->multi_query ($_sql);
       DB::UNDB ($_result = null, $_db);
     return true;
       //Get Next Value Added ID model public Function NextID ($_table) {$_sql = "show table STATUS like ' $_table '";
       $_object = $this->one ($_sql);
     Return $_object->auto_increment;
       ///Find the total record model protected function totals ($_sql) {$_db = Db::getdb ();
       $_result = $_db->query ($_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);
       //Find 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);
       }//Additions repair model protected function AUD ($_sql) {$_db = Db::getdb ();
       $_db->query ($_sql);
       $_affected_rows = $_db->affected_rows;
       DB::UNDB ($_result = null, $_db);
     return $_affected_rows;

 }}?>

More about PHP Interested readers can view the site topics: "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary ( including word,excel,access,ppt), the "Summary of PHP date and time usage", "PHP Introduction to Object-oriented Programming", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

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.