A new PDO transaction instance based on PDO database operation class of

  • 2020-05-09 18:20:01
  • OfStack

 
<?php 
/* 
*  Author: hu rui  
*  Date: 2011/03/19 
*  Email: hooray0905@foxmail.com 
* 
* 20110319 
*  Common database operations, such as: add, delete, change, get a single record, multiple records, return the latest 1 Bar insertion record id , returns the number of rows of operation records, etc  
* 20110630 
*  Modify the method as a whole and merge some parameters  
*  Specification code, 1 There's only one way 1 a return statements  
*/ 
/* 
 Parameters that  
int $debug  Whether to turn on debugging, turn on output sql statements  
int $mode 0  Returns an array of  
1  Returns a single record  
2  Returns the number of rows  
string $table  The database table  
string $fields  The database fields that need to be queried are allowed to be empty and all are found by default  
string $sqlwhere  Query condition, allowed to be empty  
string $orderby  Sort, allowed to be null, defaults to 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; 
} 
} 
/* 
 Parameters that  
int $debug  Whether to turn on debugging, turn on output sql statements  
int $mode 0  The default insert , no information is returned  
1  Returns the number of execution entries  
2  Returns the last 1 Subinsert record id 
string $table  The database table  
string $fields  The fields that need to be inserted into the database  
string $values  Need to insert database information, must with $fields11 The corresponding  
*/ 
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; 
} 
} 
/* 
 Parameters that  
int $debug  Whether to turn on debugging, turn on output sql statements  
int $mode 0  The default update , no information is returned  
1  Returns the number of execution entries  
string $table  The database table  
string $set  Field and content to be updated, format: a='abc',b=2,c='2010-10-10 10:10:10' 
string $sqlwhere  Modify the condition to allow null  
*/ 
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; 
} 
} 
/* 
 Parameters that  
int $debug  Whether to turn on debugging, turn on output sql statements  
int $mode 0  The default delete , no information is returned  
1  Returns the number of execution entries  
string $table  The database table  
string $sqlwhere  Delete condition, allow to be empty  
*/ 
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; 
} 
} 
?> 

Another piece of code is based on the transaction instance of my database operation class:
 
/* 
 Note that the database operation table type must be InnoDB , other types do not support transactions  
PDO Transaction mechanism  
$pdo->beginTransaction(); -- Open the transaction  
$pdo->commit(); -- End of the transaction  
$pdo->rollBack(); -- A rollback operation  

 The sample, try/catch encase db Operation when within a transaction db When the operation breaks, a rollback is performed and an exception message is 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 download

Related articles: