日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 单例模式的类,用单例模式来设计一个PHP数据库类

發(fā)布時間:2023/12/4 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 单例模式的类,用单例模式来设计一个PHP数据库类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

下面的類也可以參考下:

/*

* mysql 單例

*/

class mysql{

private $host ='localhost'; //數(shù)據(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ù)庫連接標識;

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

}

?>

總結(jié)

以上是生活随笔為你收集整理的php 单例模式的类,用单例模式来设计一个PHP数据库类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。