欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一個(gè)基于PDO的數(shù)據(jù)庫(kù)操作類(lèi)(新) 一個(gè)PDO事務(wù)實(shí)例

 更新時(shí)間:2011年07月03日 21:59:47   作者:  
原先已經(jīng)寫(xiě)過(guò)一個(gè)PDO的數(shù)據(jù)庫(kù)操作類(lèi),這次只是在原先基礎(chǔ)上進(jìn)行修改。
復(fù)制代碼 代碼如下:

<?php
/*
* 作者:胡睿
* 日期:2011/03/19
* 電郵:hooray0905@foxmail.com
*
* 20110319
* 常用數(shù)據(jù)庫(kù)操作,如:增刪改查,獲取單條記錄、多條記錄,返回最新一條插入記錄id,返回操作記錄行數(shù)等
* 20110630
* 整體修改方法,合并部分參數(shù)
* 規(guī)范代碼,一個(gè)方法里只有1個(gè)return語(yǔ)句
*/
/*
參數(shù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $mode 0 返回?cái)?shù)組
1 返回單條記錄
2 返回行數(shù)
string $table 數(shù)據(jù)庫(kù)表
string $fields 需要查詢(xún)的數(shù)據(jù)庫(kù)字段,允許為空,默認(rèn)為查找全部
string $sqlwhere 查詢(xún)條件,允許為空
string $orderby 排序,允許為空,默認(rèn)為id倒序
*/
function hrSelect($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="id desc"){
global $pdo;
if($debug){
if($mode == 2){
echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";
}elseif($mode == 1){
echo "select $fields from $table where 1=1 $sqlwhere";
}else{
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
exit;
}else{
if($mode == 2){
$rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");
$return = $rs->fetchColumn();
}elseif($mode == 1){
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere");
$return = $rs->fetch();
}else{
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
$return = $rs->fetchAll();
}
return $return;
}
}
/*
參數(shù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $mode 0 默認(rèn)insert,無(wú)返回信息
1 返回執(zhí)行條目數(shù)
2 返回最后一次插入記錄的id
string $table 數(shù)據(jù)庫(kù)表
string $fields 需要插入數(shù)據(jù)庫(kù)的字段
string $values 需要插入數(shù)據(jù)庫(kù)的信息,必須與$fields一一對(duì)應(yīng)
*/
function hrInsert($debug, $mode, $table, $fields, $values){
global $pdo;
if($debug){
echo "insert into $table ($fields) values ($values)";
exit;
}else{
if($mode == 2){
$return = $pdo->lastInsertId("insert into $table ($fields) values ($values)");
}elseif($mode == 1){
$return = $pdo->exec("insert into $table ($fields) values ($values)");
}else{
$pdo->query("insert into $table ($fields) values ($values)");
exit;
}
return $return;
}
}
/*
參數(shù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $mode 0 默認(rèn)update,無(wú)返回信息
1 返回執(zhí)行條目數(shù)
string $table 數(shù)據(jù)庫(kù)表
string $set 需要更新的字段及內(nèi)容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
string $sqlwhere 修改條件,允許為空
*/
function hrUpdate($debug, $mode, $table, $set, $sqlwhere=""){
global $pdo;
if($debug){
echo "update $table set $set where 1=1 $sqlwhere";
exit;
}else{
if($mode==1){
$return = $pdo->exec("update $table set $set where 1=1 $sqlwhere");
}else{
$pdo->query("update $table set $set where 1=1 $sqlwhere");
exit;
}
return $return;
}
}
/*
參數(shù)說(shuō)明
int $debug 是否開(kāi)啟調(diào)試,開(kāi)啟則輸出sql語(yǔ)句
int $mode 0 默認(rèn)delete,無(wú)返回信息
1 返回執(zhí)行條目數(shù)
string $table 數(shù)據(jù)庫(kù)表
string $sqlwhere 刪除條件,允許為空
*/
function hrDelete($debug, $mode, $table, $sqlwhere=""){
global $pdo;
if($debug){
echo "delete from $table where 1=1 $sqlwhere";
exit;
}else{
if($mode == 1){
$return = $pdo->exec("delete from $table where 1=1 $sqlwhere");
}else{
$pdo->query("delete from $table where 1=1 $sqlwhere");
exit;
}
return $return;
}
}
?>

另外一段代碼是基于我這個(gè)數(shù)據(jù)庫(kù)操作類(lèi)的事務(wù)實(shí)例:
復(fù)制代碼 代碼如下:

/*
注意,數(shù)據(jù)庫(kù)操作表類(lèi)型必須為InnoDB,其他類(lèi)型不支持事務(wù)
PDO事務(wù)機(jī)制
$pdo->beginTransaction(); --開(kāi)啟事務(wù)
$pdo->commit(); --結(jié)束事務(wù)
$pdo->rollBack(); --回滾操作

示例,用try/catch包住db操作,當(dāng)事務(wù)內(nèi)的db操作出現(xiàn)中斷,則執(zhí)行回滾并拋出異常信息。
*/
try{
$pdo->beginTransaction();
hrInsert(0,1,"class","name,parentid","'god',0"); //可以正常執(zhí)行
hrInsert(0,0,0,"tb_searchlog","userid,code","4"); //出錯(cuò)
$pdo->commit();
}catch(Exception $e){
$pdo->rollBack();
echo "Failed: " . $e->getMessage();
}

代碼下載:點(diǎn)擊下載

相關(guān)文章

最新評(píng)論