一個(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)擊下載
您可能感興趣的文章:
- php的PDO事務(wù)處理機(jī)制實(shí)例分析
- PHP中PDO的事務(wù)處理分析
- php下pdo的mysql事務(wù)處理用法實(shí)例
- php中在PDO中使用事務(wù)(Transaction)
- php通過(guò)PHPExcel導(dǎo)入Excel表格到MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
- thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
- php導(dǎo)出word文檔與excel電子表格的簡(jiǎn)單示例代碼
- PHP處理excel cvs表格的方法實(shí)例介紹
- PHP5中使用PDO連接數(shù)據(jù)庫(kù)的方法
- PHP PDO函數(shù)庫(kù)詳解
- Php中用PDO查詢(xún)Mysql來(lái)避免SQL注入風(fēng)險(xiǎn)的方法
- 全新的PDO數(shù)據(jù)庫(kù)操作類(lèi)php版(僅適用Mysql)
- php使用PDO事務(wù)配合表格讀取大量數(shù)據(jù)插入操作實(shí)現(xiàn)方法
相關(guān)文章
phpStudy中升級(jí)MySQL版本到5.7.17的方法步驟
這篇文章主要給大家介紹了關(guān)于phpStudy中升級(jí)MySQL版本到5.7.17的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08PHP 使用二進(jìn)制保存用戶狀態(tài)的實(shí)例
下面小編就為大家分享一篇PHP 使用二進(jìn)制保存用戶狀態(tài)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01PHP實(shí)現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn)
這篇文章主要介紹了PHP實(shí)現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn),涉及php針對(duì)環(huán)形鏈表的遍歷、查找、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01在windows服務(wù)器開(kāi)啟php的gd庫(kù)phpinfo中未發(fā)現(xiàn)
在windows服務(wù)器開(kāi)啟php的gd庫(kù)時(shí),使用cgi之后phpinfo()得到的結(jié)果中 Configure Command 中并沒(méi)有出現(xiàn)gd,很是疑惑,于是搜集了一些,希望對(duì)你們有幫助,感興趣的朋友可以了解下2013-01-01PHP中mysqli_get_server_version()的實(shí)例用法
在本篇文章里小編給大家分享的是關(guān)于PHP中mysqli_get_server_version()用法以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考下。2020-02-02如何解決php domdocument找不到的問(wèn)題
在本篇文章里小編給大家整理的是一篇關(guān)于php domdocument找不到的解決辦法,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-07-07