一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - 全新的PDO數據庫操作類php版(僅適用Mysql)

全新的PDO數據庫操作類php版(僅適用Mysql)

2020-01-05 17:24胡尐睿丶 PHP教程

在公司里也用了1年之久。如今公司規模變大了,產品也日益完善,曾經的那個數據庫操作函數雖說使用上沒出什么大問題,但為了更顯專業,花了1天時間重寫了這個,現在,它確實是個類了

代碼如下:


/** 
* 作者:胡睿 
* 日期:2012/07/21 
* 電郵:[email protected] 
*/ 

class HRDB{ 
protected $pdo; 
protected $res; 
protected $config; 

/*構造函數*/ 
function __construct($config){ 
$this->Config = $config; 
$this->connect(); 


/*數據庫連接*/ 
public function connect(){ 
$this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']); 
$this->pdo->query('set names utf8;'); 
//把結果序列化成stdClass 
//$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 
//自己寫代碼捕獲Exception 
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


/*數據庫關閉*/ 
public function close(){ 
$this->pdo = null; 


public function query($sql){ 
$res = $this->pdo->query($sql); 
if($res){ 
$this->res = $res; 


public function exec($sql){ 
$res = $this->pdo->exec($sql); 
if($res){ 
$this->res = $res; 


public function fetchAll(){ 
return $this->res->fetchAll(); 

public function fetch(){ 
return $this->res->fetch(); 

public function fetchColumn(){ 
return $this->res->fetchColumn(); 

public function lastInsertId(){ 
return $this->res->lastInsertId(); 


/** 
* 參數說明 
* int $debug 是否開啟調試,開啟則輸出sql語句 
* 0 不開啟 
* 1 開啟 
* 2 開啟并終止程序 
* int $mode 返回類型 
* 0 返回多條記錄 
* 1 返回單條記錄 
* 2 返回行數 
* string/array $table 數據庫表,兩種傳值模式 
* 普通模式: 
* 'tb_member, tb_money' 
* 數組模式: 
* array('tb_member', 'tb_money') 
* string/array $fields 需要查詢的數據庫字段,允許為空,默認為查找全部,兩種傳值模式 
* 普通模式: 
* 'username, password' 
* 數組模式: 
* array('username', 'password') 
* string/array $sqlwhere 查詢條件,允許為空,兩種傳值模式 
* 普通模式: 
* 'and type = 1 and username like "%os%"' 
* 數組模式: 
* array('type = 1', 'username like "%os%"') 
* string $orderby 排序,默認為id倒序 
*/ 
public function select($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){ 
//參數處理 
if(is_array($table)){ 
$table = implode(', ', $table); 

if(is_array($fields)){ 
$fields = implode(', ', $fields); 

if(is_array($sqlwhere)){ 
$sqlwhere = ' and '.implode(' and ', $sqlwhere); 

//數據庫操作 
if($debug === 0){ 
if($mode === 2){ 
$this->query("select count(tbid) from $table where 1=1 $sqlwhere"); 
$return = $this->fetchColumn(); 
}else if($mode === 1){ 
$this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); 
$return = $this->fetch(); 
}else{ 
$this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby"); 
$return = $this->fetchAll(); 

return $return; 
}else{ 
if($mode === 2){ 
echo "select count(tbid) from $table where 1=1 $sqlwhere"; 
}else if($mode === 1){ 
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"; 

else{ 
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"; 

if($debug === 2){ 
exit; 




/** 
* 參數說明 
* int $debug 是否開啟調試,開啟則輸出sql語句 
* 0 不開啟 
* 1 開啟 
* 2 開啟并終止程序 
* int $mode 返回類型 
* 0 無返回信息 
* 1 返回執行條目數 
* 2 返回最后一次插入記錄的id 
* string/array $table 數據庫表,兩種傳值模式 
* 普通模式: 
* 'tb_member, tb_money' 
* 數組模式: 
* array('tb_member', 'tb_money') 
* string/array $set 需要插入的字段及內容,兩種傳值模式 
* 普通模式: 
* 'username = "test", type = 1, dt = now()' 
* 數組模式: 
* array('username = "test"', 'type = 1', 'dt = now()') 
*/ 
public function insert($debug, $mode, $table, $set){ 
//參數處理 
if(is_array($table)){ 
$table = implode(', ', $table); 

if(is_array($set)){ 
$set = implode(', ', $set); 

//數據庫操作 
if($debug === 0){ 
if($mode === 2){ 
$this->query("insert into $table set $set"); 
$return = $this->lastInsertId(); 
}else if($mode === 1){ 
$this->exec("insert into $table set $set"); 
$return = $this->res; 
}else{ 
$this->query("insert into $table set $set"); 
$return = NULL; 

return $return; 
}else{ 
echo "insert into $table set $set"; 
if($debug === 2){ 
exit; 




/** 
* 參數說明 
* int $debug 是否開啟調試,開啟則輸出sql語句 
* 0 不開啟 
* 1 開啟 
* 2 開啟并終止程序 
* int $mode 返回類型 
* 0 無返回信息 
* 1 返回執行條目數 
* string $table 數據庫表,兩種傳值模式 
* 普通模式: 
* 'tb_member, tb_money' 
* 數組模式: 
* array('tb_member', 'tb_money') 
* string/array $set 需要更新的字段及內容,兩種傳值模式 
* 普通模式: 
* 'username = "test", type = 1, dt = now()' 
* 數組模式: 
* array('username = "test"', 'type = 1', 'dt = now()') 
* string/array $sqlwhere 修改條件,允許為空,兩種傳值模式 
* 普通模式: 
* 'and type = 1 and username like "%os%"' 
* 數組模式: 
* array('type = 1', 'username like "%os%"') 
*/ 
public function update($debug, $mode, $table, $set, $sqlwhere=""){ 
//參數處理 
if(is_array($table)){ 
$table = implode(', ', $table); 

if(is_array($set)){ 
$set = implode(', ', $set); 

if(is_array($sqlwhere)){ 
$sqlwhere = ' and '.implode(' and ', $sqlwhere); 

//數據庫操作 
if($debug === 0){ 
if($mode === 1){ 
$this->exec("update $table set $set where 1=1 $sqlwhere"); 
$return = $this->res; 
}else{ 
$this->query("update $table set $set where 1=1 $sqlwhere"); 
$return = NULL; 

return $return; 
}else{ 
echo "update $table set $set where 1=1 $sqlwhere"; 
if($debug === 2){ 
exit; 




/** 
* 參數說明 
* int $debug 是否開啟調試,開啟則輸出sql語句 
* 0 不開啟 
* 1 開啟 
* 2 開啟并終止程序 
* int $mode 返回類型 
* 0 無返回信息 
* 1 返回執行條目數 
* string $table 數據庫表 
* string/array $sqlwhere 刪除條件,允許為空,兩種傳值模式 
* 普通模式: 
* 'and type = 1 and username like "%os%"' 
* 數組模式: 
* array('type = 1', 'username like "%os%"') 
*/ 
public function delete($debug, $mode, $table, $sqlwhere=""){ 
//參數處理 
if(is_array($sqlwhere)){ 
$sqlwhere = ' and '.implode(' and ', $sqlwhere); 

//數據庫操作 
if($debug === 0){ 
if($mode === 1){ 
$this->exec("delete from $table where 1=1 $sqlwhere"); 
$return = $this->res; 
}else{ 
$this->query("delete from $table where 1=1 $sqlwhere"); 
$return = NULL; 

return $return; 
}else{ 
echo "delete from $table where 1=1 $sqlwhere"; 
if($debug === 2){ 
exit; 




其實使用上,和之前的相差不大,目的就是為了方便移植。 

  本次重寫著重處理了幾個問題: 

  ① insert語句太復雜,fields與values對應容易出現誤差 

  我們看下最常見的一句sql插入語句 

復制代碼代碼如下:

insert into tb_member (username, type, dt) values ('test', 1, now())


  在傳統模式下,fields和values參數是分開傳入的,但卻要保證兩者參數傳入的順序一致。這很容易導致順序錯亂或者漏傳某個參數。 

  這次已經把問題修改了,采用了mysql獨有的insert語法,同樣是上面那功能,就可以換成這樣的寫法 

復制代碼代碼如下:

insert into tb_member set username = "test", type = 1, lastlogindt = now()


  就像update一樣,一目了然。 

  ② 部分參數可以用數組代替 

  比如這樣一句sql 

復制代碼代碼如下:

delete from tb_member where 1=1 and tbid = 1 and username = "hooray"


  在原先調用方法的時候,需要手動拼裝好where條件,這樣操作的成本很高,現在完全可以用這種形式 

復制代碼代碼如下:


$where = array( 
'tbid = 1', 
'username = "hooray"' 
); 
$db->delete(1, 0, 'tb_member', $where); 


  條件再多也不會打亂你的思路。同樣,不僅僅是where參數,update里的set也可以以這種形式(具體可參見完整源碼) 

復制代碼代碼如下:


$set = array('username = "123"', 'type = 1', 'lastlogindt = now()'); 
$where = array('tbid = 1'); 
$db->update(1, 0, 'tb_member', $set, $where); 


  ③ 可自定義sql語句 

  有時候,sql過于復雜,導致無法使用類里提供的方法去組裝sql語句,這時候就需要一個功能,就是能直接傳入我已經組裝好的sql語句執行,并返回信息。現在,這功能也有了 

復制代碼代碼如下:


$db->query('select username, password from tb_member'); 
$rs = $db->fetchAll(); 


  是不是很像pdo原生態的寫法? 

  ④ 支持創建多數據庫連接 

  原先的因為只是數據庫操作方法,所以并不支持多數據庫連接,在實現上需要復制出2個相同的文件,修改部分變量,操作實屬復雜。現在這問題也解決了。 

復制代碼代碼如下:


$db_hoorayos_config = array( 
'dsn'=>'mysql:host=localhost;dbname=hoorayos', 
'name'=>'root', 
'password'=>'hooray' 
); 
$db = new HRDB($db_hoorayos_config); 

$db_hoorayos_config2 = array( 
'dsn'=>'mysql:host=localhost;dbname=hoorayos2', 
'name'=>'root', 
'password'=>'hooray' 
); 
$db2 = new HRDB($db_hoorayos_config2); 


  這樣就能同時創建2個數據庫連接,方便處理數據庫與數據庫交互的情況。 

  大致新功能就是這么多了,整個代碼并不多,歡迎閱讀了解。下面是我在編寫時寫的測試代碼,也一并提供上來,方便大家學習。 

復制代碼代碼如下:


require_once('global.php'); 
require_once('inc/setting.inc.php'); 

$db = new HRDB($db_hoorayos_config); 

echo '<hr><b>select測試</b><hr>'; 
echo '普通模式,直接字符串傳入<br>'; 
$rs = $db->select(1, 0, 'tb_member', 'username, password', 'and type = 1 and username like "%os%"'); 
echo '<br>數組模式,可傳入數組<br>'; 
$fields = array('username', 'password'); 
$where = array('type = 1', 'username like "%os%"'); 
$rs = $db->select(1, 0, 'tb_member', $fields, $where); 

echo '<hr><b>insert測試</b><hr>'; 
echo '普通模式,直接字符串傳入<br>'; 
$db->insert(1, 0, 'tb_member', 'username = "test", type = 1, lastlogindt = now()'); 
echo '<br>數組模式,可傳入數組<br>'; 
$set = array('username = "test"', 'type = 1', 'lastlogindt = now()'); 
$db->insert(1, 0, 'tb_member', $set); 

echo '<hr><b>update測試</b><hr>'; 
echo '普通模式,直接字符串傳入<br>'; 
$db->update(1, 0, 'tb_member', 'username = "123", type = 1, lastlogindt = now()', 'and tbid = 7'); 
echo '<br>數組模式,可傳入數組<br>'; 
$set = array('username = "123"', 'type = 1', 'lastlogindt = now()'); 
$where = array('tbid = 1'); 
$db->update(1, 0, 'tb_member', $set, $where); 

echo '<hr><b>delete測試</b><hr>'; 
echo '普通模式,直接字符串傳入<br>'; 
$db->delete(1, 0, 'tb_member', 'and tbid = 1 and username = "hooray"'); 
echo '<br>數組模式,可傳入數組<br>'; 
$where = array( 
'tbid = 1', 
'username = "hooray"' 
); 
$db->delete(1, 0, 'tb_member', $where); 

echo '<hr><b>自定義sql</b><hr>'; 
$db->query('select username, password from tb_member'); 
$rs = $db->fetchAll(); 
var_dump($rs); 

$db->close(); 


作者:胡尐睿丶

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩毛片网 | 男人捅女人动漫 | fulao在线观看的| 久久综合网久久综合 | 亚洲狠狠综合久久 | 国产一区二区三区日韩 | 欧美日本一本线在线观看 | 国产农村乱子伦精品视频 | 波多野结衣178部中文字幕 | 韩国三级年轻小的胰子完整 | 特黄特黄一级高清免费大片 | 天堂激情网 | 香蕉久久ac一区二区三区 | 男人肌肌捅女人肌肌 | 亚洲欧美日韩成人一区在线 | 国产成人夜色影视视频 | 国产视频播放 | 欧美做受 | 天天爽天天干天天操 | 爽好舒服快想要免费看 | 万域之王动漫在线观看全集免费播放 | 色导行 | 日本韩国推理片免费观看网站 | 精品日韩欧美一区二区三区在线播放 | 日本精品久久久久久久久免费 | 侮辱丰满美丽的人妻 | 免费福利资源站在线视频 | 午夜精品在线 | 四虎影库网址 | chinese一bdsmⅹxx chinese帅男gayvideo | 亚洲免费在线观看视频 | 糖心vlog麻豆精东影业传媒 | 欧美日韩一区二区三区免费不卡 | 99精品热 | 午夜小视频网站 | fquer老师 | 日本大尺度动漫在线观看缘之空 | videos护士有奶水 | 美女扒开腿让男人桶爽动态图片 | 男人天堂a | 短篇同学新婚h系列小说 |