Mysql class implemented by PHP based on Singleton mode

Source: Internet
Author: User
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 );}}

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.