php 單例 數(shù)據(jù)庫

1********

class nmdb

{

private $link;

static private $_instance;

// 連接數(shù)據(jù)庫

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;

}

// 連接數(shù)據(jù)表

public function select_db($database)

{

$this->result = mysql_select_db($database);

return $this->result;

}

// 執(zhí)行SQL語句

public function query($query)

{

return $this->result = mysql_query($query, $this->link);

}

// 將結(jié)果集保存為數(shù)組

public function fetch_array($fetch_array)

{

return $this->result = mysql_fetch_array($fetch_array, MYSQL_ASSOC);

}

// 獲得記錄數(shù)目

public function num_rows($query)

{

return $this->result = mysql_num_rows($query);

}

// 關(guān)閉數(shù)據(jù)庫連接

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'; //數(shù)據(jù)庫主機(jī)

private $user? ? = 'root'; //數(shù)據(jù)庫用戶名

private $pwd? ? = ''; //數(shù)據(jù)庫用戶名密碼

private $database = 'imoro_imoro'; //數(shù)據(jù)庫名

private $charset = 'utf8'; //數(shù)據(jù)庫編碼,GBK,UTF8,gb2312

private $link;? ? ? ? ? ? //數(shù)據(jù)庫連接標(biāo)識;

private $rows;? ? ? ? ? ? //查詢獲取的多行數(shù)組

static $_instance; //存儲對象

/**

* 構(gòu)造函數(shù)

* 私有

*/

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

}

?>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容