pcskys_windows7loader 一個基於PDO的資料庫操作類別新 一個PDO事務實例

WBOY
發布: 2016-07-29 08:45:55
原創
986 人瀏覽過

複製程式碼 程式碼如下:


/*
* 作者:胡睿
php
/*
* 作者:胡睿* 日期:2011/03/19
* 電子郵件:hooray0905@foxmail.com
*
* 20110319
* 常用資料庫操作,如:增刪一筆插入記錄id,回傳操作記錄行數等
* 20110630
* 整體修改方法,合併部分參數
* 規範代碼,一個方法裡只有1個return語句
*/
/*
參數說明
int $debug 是否開啟偵錯,開啟則輸出sql語句
int $mode 0 傳回陣列
1 傳回單一記錄
2 傳回行數
string $ table 資料庫表
string $fields 需要查詢的資料庫字段,允許為空,預設為查找全部
string $sqlwhere 查詢條件,允許為空
string $orderby 排序,允許為空,預設為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;
}
}
/*
參數說明
int $debug 是否開啟調試,開啟則輸出sql語句
int $mode 0 預設insert,無回傳訊息
1傳回執行條目數
2 傳回最後一次插入記錄的id
string $table 資料庫表
string $fields 需要插入資料庫的欄位
string $values 需要插入資料庫的信息,必須與$fields一一對應
*/
function hrInsert($debug, $mode, $table, $fields, $values){
global $pdo;
if($de){ echobal $pdo;
if($debug){ echobal $pdo;
if($de){ echobal $pdo;
if($de){ 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;
}
}
/*
參數說明
int $debug 是否開啟調試,開啟則輸出sql語句
int $mode 0 預設update ,無回傳資訊
1 傳回執行條目數
string $table 資料庫表
string $set 需要更新的欄位及內容,格式: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;
}
}
/*
參數說明
int $debug 是否開啟偵錯,開啟則輸出sql語句
int $mode 0 預設delete,無回傳資訊
1 傳回執行條目數
string $table 資料庫表
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;

}
}
?>

另外一段程式碼是基於我這個資料庫操作類別的事務實例:

複製程式碼

程式碼如下:


/*
注意,資料庫操作表類型必須為InnoDB,其他類型不支援交易
PDO事務機制
$pdo->beginTransaction(); --開啟交易
$pdo->commit(); --結束交易
$pdo->rollBack(); --回滾操作
範例,用try/catch包住db操作,當事務內的db操作出現中斷,則執行回滾並拋出異常訊息。
*/
try{
$pdo->beginTransaction();
hrInsert(0,1,"class","name,parentid","'god',0"); / /可以正常執行
hrInsert(0,0,0,"tb_searchlog","userid,code","4"); //出錯
$pdo->commit();
}catch( Exception $e){

$pdo->rollBack(); echo "Failed: " . $e->getMessage(); }
代碼下載:點選下載

以上就介紹了pcskys_windows7loader 一個基於PDO的資料庫操作類新 一個PDO事務實例,包括了pcskys_windows7loader方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!