A pdo-based database operation class (new) and a PDO transaction instance. Copy the code as follows :? Php ** author: Hu Rui * Date: 20110319 * Email: hooray0905@foxmail.com ** 20110319 * common database operations, such as: add, delete, modify, query, get a single record
The code is as follows:
/*
* Author: Hu Rui
* Date: 2011/03/19
* Email: hooray0905@foxmail.com
*
* 20110319
* Common database operations, such as adding, deleting, modifying, and querying, obtaining a single record and multiple records, returning the latest insert record id and the number of Operation records
* 20110630
* Overall modification method, merging some parameters
* Standard code. there is only one return statement in a method.
*/
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Int $ mode 0 returns an array
1. return a single record
2. number of returned rows
String $ table database table
String $ fields: the database field to be queried. it can be null. the default value is to search for all fields.
String $ sqlwhere query condition, which can be null
String $ orderby sorting, which can be null. the default value is id reverse.
*/
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
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Int $ mode 0 default insert, No returned information
1. number of execution entries returned
2. return the id of the last inserted record.
String $ table database table
String $ fields to be inserted into the database
String $ values: The information of the database to be inserted. it 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
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Int $ mode 0 default update, no returned information
1. number of execution entries returned
String $ table database table
String $ set field and content to be updated. format: a = 'abc', B = 2, c = '2017-10-10 10:10:10'
String $ sqlwhere condition for modification, which can be 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
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Int $ mode 0 default delete, no returned information
1. number of execution entries returned
String $ table database table
String $ sqlwhere deletion condition, which can be 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 a transaction instance based on my database operation class:
The code is as follows:
/*
Note: The database operation table must be of the InnoDB type, and other types do not support transactions.
PDO transaction mechanism
$ Pdo-> beginTransaction (); -- start the transaction
$ Pdo-> commit (); -- ends the transaction
$ Pdo-> rollBack (); -- rollBack operation
For example, use try/catch to wrap the db operation. when the db operation in the transaction is interrupted, roll back and throw exception information.
*/
Try {
$ Pdo-> beginTransaction ();
HrInsert (, "class", "name, parentid", "'God', 0"); // it 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 ();
}
Download Code: Click to download
The http://www.bkjia.com/PHPjc/323700.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323700.htmlTechArticle code is as follows :? Php/** author: Hu Rui * Date: 2011/03/19 * Email: hooray0905@foxmail.com ** 20110319 * common database operations, such as: add, delete, modify, query, get a single record...