PHP is a single-sample database operation base class. PHP database operation base class based on the Singleton mode. This document describes the database operation base class implemented by php based on the Singleton mode. For your reference, PHP is based on the database operation base class implemented in the Singleton mode, and php database in the example mode.
This document describes the basic database operation classes implemented by PHP based on the Singleton mode. We will share this with you for your reference. The details are as follows:
Configuration File:
<?php$db = array( 'host'=>'localhost', 'user'=>'root', 'password'=>'', 'database'=>'test',)?>
Php database base class:
<? Phpclass db {public $ conn; public static $ SQL; public static $ instance = null; private function _ construct () {require_once ('Db. config. php '); $ this-> conn = mysql_connect ($ db ['host'], $ db ['user'], $ db ['password']); if (! Mysql_select_db ($ db ['database'], $ this-> conn) {echo "failed" ;}; mysql_query ('set names utf8 ', $ this-> conn);} public static function getInstance () {if (is_null (self ::$ instance) {self ::$ instance = new db;} return self:: $ instance;}/*** query database */public function select ($ table, $ condition = array (), $ field = array ()) {$ where = ''; if (! Empty ($ condition) {foreach ($ condition as $ k => $ v) {$ where. = $ k. "= '". $ v. "'and";} $ where = 'where '. $ where. '1 = 1';} $ fieldstr = ''; if (! Empty ($ field) {foreach ($ field as $ k => $ v) {$ fieldstr. = $ v. ',';} $ fieldstr = rtrim ($ fieldstr, ',');} else {$ fieldstr = '*';} self :: $ SQL = "select {$ fieldstr} from {$ table} {$ where}"; $ result = mysql_query (self ::$ SQL, $ this-> conn ); $ resuleRow = array (); $ I = 0; while ($ row = mysql_fetch_assoc ($ result) {foreach ($ row as $ k => $ v) {$ resuleRow [$ I] [$ k] = $ v;} $ I ++;} return $ resuleRow;}/*** add a record */public Function insert ($ table, $ data) {$ values = ''; $ datas =''; foreach ($ data as $ k = >$ v) {$ values. = $ k. ','; $ datas. = "'$ V '". ',';} $ values = rtrim ($ values, ','); $ datas = rtrim ($ datas, ','); self :: $ SQL = "insert into {$ table} ({$ values}) VALUES ({$ datas})"; if (mysql_query (self: $ SQL )) {return mysql_insert_id () ;}else {return false ;};}/ *** modify a record */public function update ($ table, $ data, $ condition = array ()) {$ Where = ''; if (! Empty ($ condition) {foreach ($ condition as $ k => $ v) {$ where. = $ k. "= '". $ v. "'and";} $ where = 'where '. $ where. '1 = 1';} $ updatastr = ''; if (! Empty ($ data) {foreach ($ data as $ k => $ v) {$ updatastr. = $ k. "= '". $ v. "'," ;}$ updatastr = 'set '. rtrim ($ updatastr, ',');} self: $ SQL = "update {$ table }{$ updatastr }{$ where}"; return mysql_query (self :: $ SQL);}/*** delete record */public function delete ($ table, $ condition) {$ where = ''; if (! Empty ($ condition) {foreach ($ condition as $ k => $ v) {$ where. = $ k. "= '". $ v. "'and";} $ where = 'where '. $ where. '1 = 1';} self: $ SQL = "delete from {$ table} {$ where}"; return mysql_query (self ::$ SQL );} public static function getLastSql () {echo self: $ SQL ;}}$ db = db: getInstance (); // $ list = $ db-> select ('demo', array ('name' => 'Tom ', 'password' => 'Ds '), array ('name', 'password'); // echo $ db-> insert ('demo', array ('name' => 'customer home ', 'password' => '123'); // echo $ db-> update ('demo', array ("name" => 'XXX ', "password" => '000000'), array ('id' => 1); echo $ db-> delete ('demo ', array ('id' => '2'); db: getLastSql (); echo"";?>