首頁  >  文章  >  php教程  >  全新的PDO資料庫操作類php版

全新的PDO資料庫操作類php版

高洛峰
高洛峰原創
2016-12-02 13:55:561056瀏覽

複製程式碼 程式碼如下: 

class HRDB{ 
protected $pdo; 
protected $res; 
protected $config; 
config; 
$this->connect(); 


/*資料庫連線*/ 
public function connect(){ 
$this->pdo = new PDO($this->Config[' this->Config['name'], $this->Config['password']); 
$this->pdo->query('set names utf8;'); 
//把結果序列化成是Class 
/ /$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 
//自己寫程式碼擷取Exception 
$this->pdo->setAttribute(PDOfATTR_ERREP::R_ERREP::]


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


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


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


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

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

publicunction ->res->lastInsertId(); 


/**
* 參數說明 
* int $debug 是否開啟調試,開啟則輸出sql語句 
* 0 不開啟 
* 1 開啟 
* 2 開啟並終止程式 * int $mode 返回類型
* 2 開啟並終止程式 * int $mode 返回類型1 回傳單一記錄 
* 2 回傳行數 
* string/array $table 資料庫表,兩個傳值模式 
* 普通模式: 
* 'tb_member, tb_money' ember
* 陣列
* 'tb_member, tb_money' ember
* 陣列模式: '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="", $orderwhere="", $orderwhere="", $orderwherby ="tbid desc"){ 
//參數處理 
if(is_array($table)){ 
$table = implode(', ', $table); 

if(is_array($fields)){ 

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"; 
}nse mode === 1){ 
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby"; 

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

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




/** 
語句 開啟 $ * 0 不開啟 
* 1 開啟 
* 2 開啟並終止程式 
* int $mode 回傳類型 
* 0 無回傳訊息 
* 1 傳回執行條目數 
* 2 傳回最後一次插入記錄的記憶體數$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; 
if($debug === 2){ exit; 
if($debug === 2){ exit; }
} #&*/ 
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)){sql

if(is_array($sqlwhere)){sql
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 =n==




/**
* 參數說明 
* int $debug 是否開啟調試,開啟則輸出sql語句 
* 0 不開啟 
* 1 開啟 
* 2 開啟並終止程式傳回資訊
* int $mode 返回類型回傳執行條目數 
* string $table 資料庫表,兩個傳值模式 
* 普通模式: 
* 'tb_member, tb_money' 
* 陣列模式: 
* array('tb_member', 'tb_member' 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 delete($debug, $mode, $table, $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"; ; 





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

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

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

  我們看下最常見的一句sql_mert 

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

複製碼, 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 = "phoo" 好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(); 

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

  ④ 支援建立多資料庫連接 

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

複製碼 程式碼如下: 
$db_hoorayos_config = array( 
'dsn'=>'mysql:host=localhost;dbname=hoorayos', 
'name'=>'root', 
'pass
>
); 
$db = new HRDB($db_hoorayos_config); 

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

  這樣就能同時建立2個資料庫連接,方便處理資料庫與資料庫互動的狀況。 

  大致新功能就是這麼多了,整個程式碼不多,歡迎閱讀了解。以下是我在寫作時寫的測試程式碼,也一併提供上來,方便大家學習。

複製程式碼 程式碼如下: 
require_once('global.php'); 
require_once('inc/setting.inc.php'); 

$db select測試


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

echo '
insert測試​​
'; 
echo '普通模式,直接字串傳入
'; 
$db->insert(1, 0, ' tb_member', 'username = "test", type = 1, lastlogindt = now()'); 
echo '
陣列模式,可傳入陣列
'; 
$set = array('username = "test"', 'type = 1', 'lastlogindt = now()'); 
$db->insert(1, 0, 'tb_member', $set); 

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

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

echo '
自訂sql
'; 
$db->query('select username, password from tb_member'); 
$rs = $db->fetchAll(); 
var_dump($rs); 
-
); 
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn