最近感覺網站的數據庫壓力比較大,造成網站的速度下降得很厲害。因為有相當一部分的頁面是直接連接數據庫讀數據的,所以把這部分的頁面也改為使用數據庫單例類來實現。現在基本都統一使用下面這個類來連接數據庫了。
<?php class nmdb { private $link; static private $_instance; // 連接數據庫 private function __construct($host, $username, $password) { $this->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, $password); } return self::$_instance; } // 連接數據表 public function select_db($database) { $this->result = mysql_select_db($database); return $this->result; } // 執行SQL語句 public function query($query) { return $this->result = mysql_query($query, $this->link); } // 將結果集保存為數組 public function fetch_array($fetch_array) { return $this->result = mysql_fetch_array($fetch_array, MYSQL_ASSOC); } // 獲得記錄數目 public function num_rows($query) { return $this->result = mysql_num_rows($query); } // 關閉數據庫連接 public function close() { return $this->result = mysql_close($this->link); } } ?>
這個類的使用如下:
$connector = nmdb::get_class_nmdb($host, $username, $password); $connector -> select_db($database);
下面的類也可以參考下:
<?php /* * mysql 單例 */ class mysql{ private $host ='localhost'; //數據庫主機 private $user = 'root'; //數據庫用戶名 private $pwd = ''; //數據庫用戶名密碼 private $database = 'imoro_imoro'; //數據庫名 private $charset = 'utf8'; //數據庫編碼,GBK,UTF8,gb2312 private $link; //數據庫連接標識; private $rows; //查詢獲取的多行數組 static $_instance; //存儲對象 /** * 構造函數 * 私有 */ private function __construct($pconnect = false) { if (!$pconnect) { $this->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; } /** * 防止被克隆 * */ private function __clone(){} public static function getInstance($pconnect = false){ if(FALSE == (self::$_instance instanceof self)){ self::$_instance = new self($pconnect); } return self::$_instance; } /** * 查詢 */ public function query($sql, $link = '') { $this->result = mysql_query($sql, $this->link) or $this->err($sql); return $this->result; } /** * 單行記錄 */ public function getRow($sql, $type = MYSQL_ASSOC) { $result = $this->query($sql); return @ mysql_fetch_array($result, $type); } /** * 多行記錄 */ public function getRows($sql, $type = MYSQL_ASSOC) { $result = $this->query($sql); while ($row = @ mysql_fetch_array($result, $type)) { $this->rows[] = $row; } return $this->rows; } /** * 錯誤信息輸出 */ protected function err($sql = null) { //這裡輸出錯誤信息 echo 'error'; exit(); } } //用例 $db = mysql::getInstance(); $db2 = mysql::getInstance(); $data = $db->getRows('select * from blog'); //print_r($data); //判斷兩個對象是否相等 if($db === $db2){ echo 'true'; } ?>