This article mainly introduces the mysql class implemented by PHP based on the Singleton mode, involving the connection and query skills of PHP based on the Singleton mode encapsulation for MySQL databases, for more information, see the following example. We will share this with you for your reference. The details are as follows:
<? Phpdefined ('ACC ') | exit ('Access Denied'); // encapsulate the mysql operation class, including the connection function and query function. class mysql extends absdb {protected static $ ins = null; protected $ host; // host name protected $ user; // username protected $ passwd; // password protected $ db; // database name protected $ port; // port protected $ conn = null; // Obtain an object public static function getIns () {if (self :: $ ins = null) {self: $ ins = new self () ;}$ conf = conf: getIns (); self :: $ Ins-> host = $ conf-> host; self: $ ins-> user = $ conf-> user; self :: $ ins-> passwd = $ conf-> pwd; self: $ ins-> db = $ conf-> db; self :: $ ins-> port = $ conf-> port; self: $ ins-> connect (); self: $ ins-> select_db (); self :: $ ins-> setChar (); return self: $ ins;} // do not allow external users to perform new operations, protected function _ construct () {}// connect to the public function connect () {$ this-> conn = @ mysql_connect ($ this-> host, $ this-> user, $ this-> passwd, $ This-> port); if (! $ This-> conn) {$ error = new Exception ('database not connected', 9); throw $ error ;}// send an SQL query public function query ($ SQL) {$ rs = mysql_query ($ SQL, $ this-> conn); if (! $ Rs) {log: write ($ SQL);} return $ rs;} // encapsulate a getAll method // parameter: $ SQL // return: array, false public function getAll ($ SQL) {$ rs = $ this-> query ($ SQL); if (! $ Rs) {return false;} $ list = array (); while ($ row = mysql_fetch_assoc ($ rs) {$ list [] = $ row ;} return $ list;} // encapsulate a getRow method // parameter: $ SQL // return: array, false public function getRow ($ SQL) {$ rs = $ this-> query ($ SQL); if (! $ Rs) {return false;} return mysql_fetch_assoc ($ rs);} // encapsulate a getOne method, // parameter: $ SQL // return: int, str (single value) public function getOne ($ SQL) {$ rs = $ this-> query ($ SQL); if (! $ Rs) {return false;} $ tmp = mysql_fetch_row ($ rs); return $ tmp [0];} // encapsulate an afftect_rows () method // parameter: none // return int affected rows public function affected_rows () {return mysql_affected_rows ($ this-> conn);} // return the value of the newly generated auto_increment column public function last_id () {return mysql_insert_id ($ this-> conn);} // The public function select_db () {$ SQL = 'use '. $ this-> db; return $ this-> query ($ SQL);} // set the character set function publi C function setChar () {$ SQL = 'set names utf8'; return $ this-> query ($ SQL);} // automatically generate an insert statement, update the statement and execute the public function autoExecute ($ data, $ table, $ act = 'insert', $ where = '') {if ($ act = 'insert ') {$ SQL = 'insert '. $ table. '('; $ SQL. = implode (',', (array_keys ($ data); $ SQL. = ') values (\ ''; $ SQL. = implode ("','", array_values ($ data); $ SQL. = "')";} else if ($ act = 'update') {if (! Trim ($ where) {return false;} $ SQL = 'update '. $ table. 'set'; foreach ($ data as $ k => $ v) {$ SQL. = $ k; $ SQL. = '; $ SQL. = "'". $ v. "'," ;}$ SQL = substr ($ SQL, 0,-1); $ SQL. = 'where'; $ SQL. = $ where;} else {return false;} // return $ SQL; return $ this-> query ($ SQL );}}