Design a PHP database class in Singleton mode _ PHP Tutorial

Source: Internet
Author: User
Use the Singleton mode to design a PHP database class. Recently, I feel that the database pressure on my website is quite high, which causes the website to speed down very quickly. Because a considerable number of pages are directly connected to the database to read data, I recently felt that the database pressure on the website was relatively high, resulting in a very high speed reduction of the website. Because a considerable number of pages are directly connected to the database to read data, this part of pages is also implemented using the database singleton class. Now, the following classes are basically used to connect to the database.

 Link = mysql_connect ($ host, $ username, $ password); $ this-> query ("set names 'utf8'", $ this-> link ); // echo mysql_errno ($ this-> link ). ":". mysql_error ($ link ). "n"; // var_dump ($ this-> link); return $ this-> link;} private function _ clone () {} public static function get_class_nmdb ($ host, $ username, $ password) {// $ connector = new nmdb ($ host, $ username, $ password); // return $ connector; if (FALSE = (self :: $ _ inst Ance instanceof self) {self ::$ _ instance = new self ($ host, $ username, $ password);} return self ::$ _ instance ;} // connect to the public function select_db ($ database) {$ this-> result = mysql_select_db ($ database); return $ this-> result ;} // execute the SQL statement public function query ($ query) {return $ this-> result = mysql_query ($ query, $ this-> link );} // Save the result set as an array public function fetch_array ($ fetch_array) {return $ this-> result = mysql_fe Tch_array ($ fetch_array, MYSQL_ASSOC);} // Obtain the number of records public function num_rows ($ query) {return $ this-> result = mysql_num_rows ($ query );} // close the database connection public function close () {return $ this-> result = mysql_close ($ this-> link) ;}}?>

The usage of this class is as follows:

$connector = nmdb::get_class_nmdb($host, $username, $password);$connector -> select_db($database);

The following classes can also be referred:

 Link = @ mysql_connect ($ this-> host, $ this-> user, $ this-> pwd) or $ this-> err ();} else {$ this-> link = @ mysql_pconnect ($ this-> host, $ this-> user, $ this-> pwd) or $ this-> err ();} mysql_select_db ($ this-> database) or $ this-> err (); $ this-> query ("set names '{$ this-> charset }'", $ this-> link); return $ this-> link;}/*** prevent clone **/private function _ clone () {} public static function getInstance ($ pconnect = fa Lse) {if (FALSE = (self: $ _ instance instanceof self) {self ::$ _ instance = new self ($ pconnect);} return self :: $ _ instance;}/*** query */public function query ($ SQL, $ link = '') {$ this-> result = mysql_query ($ SQL, $ this-> link) or $ this-> err ($ SQL); return $ this-> result;}/*** single row record */public function getRow ($ SQL, $ type = MYSQL_ASSOC) {$ result = $ this-> query ($ SQL); return @ mysql_fetch_array ($ result, $ Type);}/*** multiline record */public function getRows ($ SQL, $ type = MYSQL_ASSOC) {$ result = $ this-> query ($ SQL); while ($ row = @ mysql_fetch_array ($ result, $ type )) {$ this-> rows [] = $ row;} return $ this-> rows;}/*** error message output */protected function err ($ SQL = null) {// Here, the error message echo 'error'; exit () ;}// use case $ db = mysql: getInstance (); $ db2 = mysql :: getInstance (); $ data = $ db-> getRows ('select * from blog ');/ /Print_r ($ data); // Determine whether the two objects are equal. if ($ db ===$ db2) {echo 'true' ;}?>

Bytes. Because a considerable part of the page is directly connected to the database to read data, so this part of...

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.