PHP database operation base class based on Singleton mode

Source: Internet
Author: User
: This article mainly introduces the database operation base class implemented by PHP based on the Singleton mode. if you are interested in the PHP Tutorial, refer to it. 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, $ c = 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, $ c $ 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' => 'Website ', 'password' => '123'); // echo $ db-> update ('demo', array ("name" => 'XXX ', "password" => '000000'), array ('id' => 1); echo $ db-> delete ('demo ', array ('id' => '2'); db: getLastSql (); echo"
";?>

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.