一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

復(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 是否開啟調(diào)試,開啟則輸出sql語(yǔ)句
int $mode 0 返回?cái)?shù)組
1 返回單條記錄
2 返回行數(shù)
string $table 數(shù)據(jù)庫(kù)表
string $fields 需要查詢的數(shù)據(jù)庫(kù)字段,允許為空,默認(rèn)為查找全部
string $sqlwhere 查詢條件,允許為空
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 是否開啟調(diào)試,開啟則輸出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 是否開啟調(diào)試,開啟則輸出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 是否開啟調(diào)試,開啟則輸出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ù)操作類的事務(wù)實(shí)例:
復(fù)制代碼 代碼如下:
/*
注意,數(shù)據(jù)庫(kù)操作表類型必須為InnoDB,其他類型不支持事務(wù)
PDO事務(wù)機(jī)制
$pdo->beginTransaction(); --開啟事務(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)擊下載

php技術(shù)一個(gè)基于PDO的數(shù)據(jù)庫(kù)操作類(新) 一個(gè)PDO事務(wù)實(shí)例,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 精品国产福利片在线观看 | 婷婷97| 精品久久久久香蕉网 | 国语自产免费精品视频一区二区 | 亚洲综合色色图 | 久热99这里只有精品视频6 | 九一视频在线免费观看 | 色久悠悠在线观看 | 国产成人综合久久亚洲精品 | 久久国产乱子伦精品免费一 | 日韩中文字幕一在线 | 91在线精品麻豆欧美在线 | 一级毛片成人免费看免费不卡 | 婷婷丁香六月天 | 91福利在线视频 | 亚洲国产成人久久午夜 | 丁香六月婷婷综合激情动漫 | 国内自拍视频一区二区三区 | 末成年美女黄网站色大片连接 | 亚洲伊人久久大香线蕉在观 | 在线亚洲欧洲国产综合444 | 精品久久久久久国产91 | 亚洲视频一区二区在线观看 | 色婷婷综合在线视频最新 | 在线看www免费看 | 欧美性在线播放 | 亚洲欧洲在线观看 | 婷婷综合激情网 | 国产精品第二页在线播放 | 国产一区二区在线免费观看 | 午夜视频在线播放 | 中文字幕不卡一区 | 天天干精品 | 日本www色视频 | 色老板美国在线观看 | 开心激情久久 | 在线观看污污网站 | 伊人网综合在线观看 | 久久精品视频7 | 国产日韩精品欧美在线ccc | 91原创视频|