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.