Recently feel the site of the database pressure is relatively large, resulting in the speed of the site has dropped very badly. Because a significant portion of the page is directly connected to the database read data, so this part of the page is also used to use the database singleton class to implement. Now the basic use of the following class 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::$_instance instanceof self)) {self::$_instance = new self ($host, $username, $PASSW ORD);} return self::$_instance; }//Connection data Table Public function select_db ($database) {$this->result = mysql_select_db ($database); return $this->result;} Execute 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_fetch_array ($fetch _array, MySQL _ASSOC);} Get record number public function num_rows ($query) {return $this->result = mysql_num_rows ($query);} Close database Connection Public functIon Close () {return $this->result = mysql_close ($this->link);}}? >
This class is used as follows:
$connector = Nmdb::get_class_nmdb ($host, $username, $password); $connector-select_db ($database);
The following classes can also be referred to below:
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 cloning * */Private Function __clone () {} public static function getinstance ($pconnect = False) {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-&G T;link) or $this->err ($sql); return $this->result; }/** * Single-line record */Public function GetRow ($sql, $type = Mysql_assoc) {$result = $this->query ($sql); return @Mysql_fetch_array ($result, $type); }/** * * Multi-line 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 output error message echo ' ERROR '; Exit (); }}//use case $db = Mysql::getinstance (), $db 2 = Mysql::getinstance (), $data = $db->getrows (' select * from blog ');//print_r ($ data),//Determine if two objects are equal if ($db = = = $db 2) {echo ' true ';}? >
http://www.bkjia.com/PHPjc/752473.html www.bkjia.com true http://www.bkjia.com/PHPjc/752473.html techarticle recently feel the site of the database pressure is relatively large, resulting in the speed of the site has dropped very badly. Because a considerable portion of the page is directly connected to the database read data, so put this part of the ...