Home  >  Article  >  Backend Development  >  A database operation class based on PDO (new) A PDO transaction instance_PHP tutorial

A database operation class based on PDO (new) A PDO transaction instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:27:48857browse

Copy code The code is as follows:

/*
* Author: Hu Rui
* Date: 2011/03/19
* Email: hooray0905@foxmail.com
*
* 20110319
* Common database operations, such as: add, delete, modify, query, obtain single record, multiple records, return The ID of the latest inserted record, the number of rows of returned operation records, etc.
* 20110630
* Modify the overall method and merge some parameters
* Standardize the code, there is only one return statement in a method
*/
/*
Parameter Description
int $debug Whether to enable debugging, if enabled, output sql statement
int $mode 0 Return array
1 Return single record
2 Return number of rows
string $table Database table
string $fields Database fields to be queried, allowed to be empty, the default is to find all
string $sqlwhere Query conditions, allowed to be empty
string $orderby Sorting, allowed to be empty, the default is id reverse order
*/
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;
}
}
/*
Parameter description
int $debug Whether to enable debugging, if enabled, the sql statement will be output
int $mode 0 Default insert, No return information
1 Returns the number of execution entries
2 Returns the id of the last inserted record
string $table Database table
string $fields Fields that need to be inserted into the database
string $values ​​Need to be inserted into the database The information must correspond to $fields one-to-one
*/
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)" ; Statement
int $mode 0 Default update, no return information
1 Return the number of execution entries
string $table Database table
string $set Fields and content to be updated, format: a='abc' ,b=2,c='2010-10-10 10:10:10'
string $sqlwhere Modify conditions and allow empty
*/
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;
}
}
/*
Parameter Description
int $debug Whether to enable debugging, if enabled, output sql statement
int $mode 0 Default delete, no return information
1 Return the number of execution entries
string $table database table
string $sqlwhere deletion condition, empty allowed
*/
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;
}
}
?>


Also A piece of code is based on the transaction instance of my database operation class:



Copy code

The code is as follows:

/*
Note that the database operation table type must be InnoDB, other types do not support transactions
PDO transaction mechanism
$pdo->beginTransaction(); --Enable transactions
$pdo->commit(); --End transaction
$pdo->rollBack(); --Rollback operation

Example, use try/catch to wrap db operation, when transaction If the db operation inside is interrupted, a rollback will be performed and an exception message will be thrown.
*/
try{
$pdo->beginTransaction();
hrInsert(0,1,"class","name,parentid","'god',0"); //Can be executed normally
hrInsert(0,0,0,"tb_searchlog","userid,code","4"); //Error
$pdo->commit();
} catch(Exception $e){
$pdo->rollBack();
echo "Failed: " . $e->getMessage();
}

Code download: click to download

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323700.htmlTechArticleCopy the code as follows: ?php /* * Author: Hu Rui* Date: 2011/03/19 * Email :hooray0905@foxmail.com * * 20110319 * Common database operations, such as: add, delete, modify, check, obtain single notes...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn