亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2012年07月22日 20:36:12   作者:  
在公司里也用了1年之久。如今公司規(guī)模變大了,產(chǎn)品也日益完善,曾經(jīng)的那個(gè)數(shù)據(jù)庫(kù)操作函數(shù)雖說(shuō)使用上沒(méi)出什么大問(wèn)題,但為了更顯專業(yè),花了1天時(shí)間重寫了這個(gè),現(xiàn)在,它確實(shí)是個(gè)類了
復(fù)制代碼 代碼如下:

/**
* 作者:胡睿
* 日期:2012/07/21
* 電郵:hooray0905@foxmail.com
*/

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

/*構(gòu)造函數(shù)*/
function __construct($config){
$this->Config = $config;
$this->connect();
}

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

/*數(shù)據(jù)庫(kù)關(guān)閉*/
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();
}

/**
* 參數(shù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類型
* 0 返回多條記錄
* 1 返回單條記錄
* 2 返回行數(shù)
* string/array $table 數(shù)據(jù)庫(kù)表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $fields 需要查詢的數(shù)據(jù)庫(kù)字段,允許為空,默認(rèn)為查找全部,兩種傳值模式
* 普通模式:
* 'username, password'
* 數(shù)組模式:
* array('username', 'password')
* string/array $sqlwhere 查詢條件,允許為空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數(shù)組模式:
* array('type = 1', 'username like "%os%"')
* string $orderby 排序,默認(rèn)為id倒序
*/
public function select($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){
//參數(shù)處理
if(is_array($table)){
$table = implode(', ', $table);
}
if(is_array($fields)){
$fields = implode(', ', $fields);
}
if(is_array($sqlwhere)){
$sqlwhere = ' and '.implode(' and ', $sqlwhere);
}
//數(shù)據(jù)庫(kù)操作
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;
}
}
}

/**
* 參數(shù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類型
* 0 無(wú)返回信息
* 1 返回執(zhí)行條目數(shù)
* 2 返回最后一次插入記錄的id
* string/array $table 數(shù)據(jù)庫(kù)表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $set 需要插入的字段及內(nèi)容,兩種傳值模式
* 普通模式:
* 'username = "test", type = 1, dt = now()'
* 數(shù)組模式:
* array('username = "test"', 'type = 1', 'dt = now()')
*/
public function insert($debug, $mode, $table, $set){
//參數(shù)處理
if(is_array($table)){
$table = implode(', ', $table);
}
if(is_array($set)){
$set = implode(', ', $set);
}
//數(shù)據(jù)庫(kù)操作
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;
}
}
}

/**
* 參數(shù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類型
* 0 無(wú)返回信息
* 1 返回執(zhí)行條目數(shù)
* string $table 數(shù)據(jù)庫(kù)表,兩種傳值模式
* 普通模式:
* 'tb_member, tb_money'
* 數(shù)組模式:
* array('tb_member', 'tb_money')
* string/array $set 需要更新的字段及內(nèi)容,兩種傳值模式
* 普通模式:
* 'username = "test", type = 1, dt = now()'
* 數(shù)組模式:
* array('username = "test"', 'type = 1', 'dt = now()')
* string/array $sqlwhere 修改條件,允許為空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數(shù)組模式:
* array('type = 1', 'username like "%os%"')
*/
public function update($debug, $mode, $table, $set, $sqlwhere=""){
//參數(shù)處理
if(is_array($table)){
$table = implode(', ', $table);
}
if(is_array($set)){
$set = implode(', ', $set);
}
if(is_array($sqlwhere)){
$sqlwhere = ' and '.implode(' and ', $sqlwhere);
}
//數(shù)據(jù)庫(kù)操作
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;
}
}
}

/**
* 參數(shù)說(shuō)明
* int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
* 0 不開(kāi)啟
* 1 開(kāi)啟
* 2 開(kāi)啟并終止程序
* int $mode 返回類型
* 0 無(wú)返回信息
* 1 返回執(zhí)行條目數(shù)
* string $table 數(shù)據(jù)庫(kù)表
* string/array $sqlwhere 刪除條件,允許為空,兩種傳值模式
* 普通模式:
* 'and type = 1 and username like "%os%"'
* 數(shù)組模式:
* array('type = 1', 'username like "%os%"')
*/
public function delete($debug, $mode, $table, $sqlwhere=""){
//參數(shù)處理
if(is_array($sqlwhere)){
$sqlwhere = ' and '.implode(' and ', $sqlwhere);
}
//數(shù)據(jù)庫(kù)操作
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;
}
}
}
}

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

  本次重寫著重處理了幾個(gè)問(wèn)題:

  ① insert語(yǔ)句太復(fù)雜,fields與values對(duì)應(yīng)容易出現(xiàn)誤差

  我們看下最常見(jiàn)的一句sql插入語(yǔ)句

復(fù)制代碼 代碼如下:
insert into tb_member (username, type, dt) values ('test', 1, now())

  在傳統(tǒng)模式下,fields和values參數(shù)是分開(kāi)傳入的,但卻要保證兩者參數(shù)傳入的順序一致。這很容易導(dǎo)致順序錯(cuò)亂或者漏傳某個(gè)參數(shù)。

  這次已經(jīng)把問(wèn)題修改了,采用了mysql獨(dú)有的insert語(yǔ)法,同樣是上面那功能,就可以換成這樣的寫法

復(fù)制代碼 代碼如下:
insert into tb_member set username = "test", type = 1, lastlogindt = now()

  就像update一樣,一目了然。

 ?、?部分參數(shù)可以用數(shù)組代替

  比如這樣一句sql

復(fù)制代碼 代碼如下:
delete from tb_member where 1=1 and tbid = 1 and username = "hooray"

  在原先調(diào)用方法的時(shí)候,需要手動(dòng)拼裝好where條件,這樣操作的成本很高,現(xiàn)在完全可以用這種形式
復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

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

 ?、?可自定義sql語(yǔ)句

  有時(shí)候,sql過(guò)于復(fù)雜,導(dǎo)致無(wú)法使用類里提供的方法去組裝sql語(yǔ)句,這時(shí)候就需要一個(gè)功能,就是能直接傳入我已經(jīng)組裝好的sql語(yǔ)句執(zhí)行,并返回信息?,F(xiàn)在,這功能也有了

復(fù)制代碼 代碼如下:

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

  是不是很像pdo原生態(tài)的寫法?

 ?、?支持創(chuàng)建多數(shù)據(jù)庫(kù)連接

  原先的因?yàn)橹皇菙?shù)據(jù)庫(kù)操作方法,所以并不支持多數(shù)據(jù)庫(kù)連接,在實(shí)現(xiàn)上需要復(fù)制出2個(gè)相同的文件,修改部分變量,操作實(shí)屬?gòu)?fù)雜?,F(xiàn)在這問(wèn)題也解決了。

復(fù)制代碼 代碼如下:

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

  這樣就能同時(shí)創(chuàng)建2個(gè)數(shù)據(jù)庫(kù)連接,方便處理數(shù)據(jù)庫(kù)與數(shù)據(jù)庫(kù)交互的情況。

  大致新功能就是這么多了,整個(gè)代碼并不多,歡迎閱讀了解。下面是我在編寫時(shí)寫的測(cè)試代碼,也一并提供上來(lái),方便大家學(xué)習(xí)。

復(fù)制代碼 代碼如下:

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

$db = new HRDB($db_hoorayos_config);

echo '<hr><b>select測(cè)試</b><hr>';
echo '普通模式,直接字符串傳入<br>';
$rs = $db->select(1, 0, 'tb_member', 'username, password', 'and type = 1 and username like "%os%"');
echo '<br>數(shù)組模式,可傳入數(shù)組<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測(cè)試</b><hr>';
echo '普通模式,直接字符串傳入<br>';
$db->insert(1, 0, 'tb_member', 'username = "test", type = 1, lastlogindt = now()');
echo '<br>數(shù)組模式,可傳入數(shù)組<br>';
$set = array('username = "test"', 'type = 1', 'lastlogindt = now()');
$db->insert(1, 0, 'tb_member', $set);

echo '<hr><b>update測(cè)試</b><hr>';
echo '普通模式,直接字符串傳入<br>';
$db->update(1, 0, 'tb_member', 'username = "123", type = 1, lastlogindt = now()', 'and tbid = 7');
echo '<br>數(shù)組模式,可傳入數(shù)組<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測(cè)試</b><hr>';
echo '普通模式,直接字符串傳入<br>';
$db->delete(1, 0, 'tb_member', 'and tbid = 1 and username = "hooray"');
echo '<br>數(shù)組模式,可傳入數(shù)組<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();

作者:胡尐睿丶

相關(guān)文章

  • PHP基本語(yǔ)法總結(jié)

    PHP基本語(yǔ)法總結(jié)

    這篇文章主要介紹了PHP基本語(yǔ)法總結(jié),本文從PHP能做什么開(kāi)始講解,對(duì)PHP的語(yǔ)法、注釋、變量、常量等內(nèi)容做了總結(jié),需要的朋友可以參考下
    2014-09-09
  • php支付寶手機(jī)網(wǎng)頁(yè)支付類實(shí)例

    php支付寶手機(jī)網(wǎng)頁(yè)支付類實(shí)例

    這篇文章主要介紹了php支付寶手機(jī)網(wǎng)頁(yè)支付類實(shí)例,是基于Yii框架使用的支付寶接口類文件,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • PHP實(shí)現(xiàn)微信提現(xiàn)功能

    PHP實(shí)現(xiàn)微信提現(xiàn)功能

    這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)微信提現(xiàn)功能,企業(yè)付款到用戶零錢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • PHP json_encode() 函數(shù)詳解及中文亂碼問(wèn)題

    PHP json_encode() 函數(shù)詳解及中文亂碼問(wèn)題

    這篇文章主要介紹了PHP json_encode() 函數(shù)詳解及中文亂碼問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • xml在joomla表單中的應(yīng)用詳解分享

    xml在joomla表單中的應(yīng)用詳解分享

    xml在joomla中應(yīng)用廣泛,從安裝文件配置到模版,模塊,組件,插件中都有應(yīng)用
    2012-07-07
  • 如何通過(guò)View::first使用Laravel Blade的動(dòng)態(tài)模板詳解

    如何通過(guò)View::first使用Laravel Blade的動(dòng)態(tài)模板詳解

    這篇文章主要給大家介紹了關(guān)于如何通過(guò)View::first使用Laravel Blade的動(dòng)態(tài)模板的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧。
    2017-09-09
  • Yii2組件之多圖上傳插件FileInput的詳細(xì)使用教程

    Yii2組件之多圖上傳插件FileInput的詳細(xì)使用教程

    這篇文章主要介紹了Yii2組件之多圖上傳插件FileInput的詳細(xì)使用教程的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • PHP連接SQL Server的方法分析【基于thinkPHP5.1框架】

    PHP連接SQL Server的方法分析【基于thinkPHP5.1框架】

    這篇文章主要介紹了PHP連接SQL Server的方法,結(jié)合實(shí)例形式分析了基于thinkPHP5.1框架Db類以及使用PDO進(jìn)行SQL Server數(shù)據(jù)庫(kù)連接的相關(guān)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • Smarty變量用法詳解

    Smarty變量用法詳解

    這篇文章主要介紹了Smarty變量用法,結(jié)合實(shí)例形式分析了php分配變量與配置文件讀取變量的具體使用方法,需要的朋友可以參考下
    2016-05-05
  • php把時(shí)間戳轉(zhuǎn)換成多少時(shí)間之前函數(shù)的實(shí)例

    php把時(shí)間戳轉(zhuǎn)換成多少時(shí)間之前函數(shù)的實(shí)例

    下面小編就為大家?guī)?lái)一篇php把時(shí)間戳轉(zhuǎn)換成多少時(shí)間之前函數(shù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11

最新評(píng)論