Copy Code code as follows:
<?php
/*
* Author: Hu Rui
* Date: 2011/03/19
* Email: hooray0905@foxmail.com
*
* 20110319
* Commonly used database operations, such as: Add or delete, get a single record, multiple records, return to the latest Insert record ID, return the number of operations record rows, etc.
* 20110630
* Overall modification method, merging some parameters
* Canonical code, only 1 return statements in one method
*/
/*
Parameter description
int $debug Whether debugging is turned on, then output SQL statement
int $mode 0 Return array
1 return a single record
2 Number of rows returned
String $table database table
String $fields database fields that need to be queried, allow nulls, default to find all
String $sqlwhere query condition, allow null
String $orderby sort, allow null, default to ID reverse
*/
function Hrselect ($debug, $mode, $table, $fields = "*", $sqlwhere = "", $orderby = "id desc") {
Global $pdo;
if ($debug) {
if ($mode = = 2) {
echo "SELECT COUNT (*) from the $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 debugging is turned on, then output SQL statement
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 information that needs to be inserted into the database, must correspond to $fields one by 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)");
Exit
}
return $return;
}
}
/*
Parameter description
int $debug Whether debugging is turned on, then output SQL statement
int $mode 0 Default update, no return information
1 Returns the number of execution entries
String $table database table
String $set fields and contents that need to be updated, format: a= ' abc ', b=2,c= ' 2010-10-10 10:10:10 '
String $sqlwhere Modify condition, 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;
}
}
/*
Parameter description
int $debug Whether debugging is turned on, then output SQL statement
int $mode 0 Default Delete, no return information
1 Returns the number of execution entries
String $table database table
String $sqlwhere Delete condition, allow null
*/
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 my transaction instance of the database operation class:
Copy Code code as follows:
/*
Note that the Database action table type must be InnoDB and the other types do not support transactions
PDO transaction mechanism
$pdo->begintransaction (); --Open the transaction
$pdo->commit (); --End of transaction
$pdo->rollback (); --rollback operation
example, the DB operation is wrapped with try/catch, and the rollback is performed and the exception information is thrown when the db operation within the transaction breaks.
*/
try{
$pdo->begintransaction ();
Hrinsert (0,1, "class", "Name,parentid", "' God ', 0"); can be performed correctly
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