php 單例 數據庫

1********

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);

2*******



/*

* 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';

}

?>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • //db.php /** * 單例鏈接數據庫 * */ classDb { static private$_ins...
    剛_dbac閱讀 558評論 0 0
  • 我以前,在我還在念大學的時候,我想著,將來,我要有房,我要有車,我要這樣,我要那樣,想象的欲望,一直不斷地膨脹膨脹...
    真愛521閱讀 281評論 0 0
  • 大概就像薯條泡在番茄醬里, 天上下的是帶香味兒的雨, 車開過水洼,濺起的水霧里有彩虹, 想看星星的時候關了這個世界...
    劈叉丘閱讀 169評論 0 0