Pcskys_windows7loader a PDO-based database operation Class A new instance of PDO transaction

Source: Internet
Author: User
Copy CodeThe code is as follows:


/*
* Author: Hu Rui
* Date: 2011/03/19
* e-mail: hooray0905@foxmail.com
*
* 20110319
* Common database operations, such as: Add or delete changes, get a single record, multiple records, return to the latest one insert record ID, return operation record number of rows, etc.
* 20110630
* Overall modification method, merging some parameters
* Specification code, only 1 return statements in one method
*/
/*
Parameter description
int $debug Whether debugging is turned on, output SQL statement is turned on
int $mode 0 Returns an array
1 return a single record
2 Number of rows returned
String $table database table
String $fields the database field to query, allow NULL, default to find all
String $sqlwhere query condition, allowed to be empty
String $orderby sort, allow null, default to reverse 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 the 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 the order by $orderby");
$return = $rs->fetchall ();
}
return $return;
}
}
/*
Parameter description
int $debug Whether debugging is turned on, output SQL statement is turned on
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 to insert into the database
String $values information that needs to be inserted into the database and 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, output SQL statement is turned on
int $mode 0 Default update, no return information
1 Returns the number of execution entries
String $table database table
String $set field and content to update, 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, output SQL statement is turned on
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 transactional instance of this database operation class:

Copy the Code code as follows:


/*
Note that the Database action table type must be InnoDB, and other types do not support transactions
PDO transaction mechanism
$pdo->begintransaction (); --Open transaction
$pdo->commit (); --End transaction
$pdo->rollback (); --rollback operation
example, the DB operation is wrapped with try/catch, and a rollback is performed and exception information is thrown when the db operation within the transaction is interrupted.
*/
try{
$pdo->begintransaction ();
Hrinsert (0,1, "class", "Name,parentid", "' God ', 0"); Can execute 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

The above describes the Pcskys_windows7loader a PDO-based database operation Class A new PDO transaction instance, including the content of the Pcskys_windows7loader, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.