Copy codeThe Code is as follows:
<? Php
/*
* 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:
Copy codeThe 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