復制代碼 代碼如下:
 
<?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($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; 
} 
} 
/* 
參數說明 
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(); 
} 
新聞熱點
疑難解答