A pdo-based database operation class

Source: Internet
Author: User
During the past one year of work, all projects have been using ADODB, but its code is bloated and execution efficiency is low, which leads to the need for replacement. Baidu later decided to use PDO. as to why we chose PDO, we will not talk about it more here. you will be able to understand it when you go to Baidu.
Now that you want to change the database, you need to have a common database operation class, that is, adding, deleting, modifying, and querying the database. it took a long time last night to figure out a prototype. here is the code, I hope you can give some comments.
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
*/
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Whether int $ getcount is counted. the returned value is the number of rows.
Whether int $ getrow returns a single record
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, $ getcount, $ getrow, $ table, $ fields = "*", $ sqlwhere = "", $ orderby = "id desc "){
Global $ pdo;
If ($ debug ){
If ($ getcount ){
Echo "select count (*) from $ table where 1 = 1 $ sqlwhere order by $ orderby ";
} Else {
Echo "select $ fields from $ table where 1 = 1 $ sqlwhere order by $ orderby ";
}
Exit;
} Else {
If ($ getcount ){
$ Rs = $ pdo-> query ("select count (*) from $ table where 1 = 1 $ sqlwhere order by $ orderby ");
Return $ rs-> fetchColumn ();
} Elseif ($ getrow ){
$ Rs = $ pdo-> query ("select $ fields from $ table where 1 = 1 $ sqlwhere order by $ orderby ");
Return $ rs-> fetch ();
} Else {
$ Rs = $ pdo-> query ("select $ fields from $ table where 1 = 1 $ sqlwhere order by $ orderby ");
Return $ rs-> fetchAll ();
}
}
}
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Whether int $ execrow enables the number of returned execution entries
Int $ lastinsertid whether to enable or not to return the last insert record id
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, $ execrow, $ lastinsertid, $ table, $ fields, $ values ){
Global $ pdo;
If ($ debug ){
Echo "insert into $ table ($ fields) values ($ values )";
Exit;
} Elseif ($ execrow ){
Return $ pdo-> exec ("insert into $ table ($ fields) values ($ values )");
} Elseif ($ lastinsertid ){
Return $ pdo-> lastInsertId ("insert into $ table ($ fields) values ($ values )");
} Else {
$ Pdo-> query ("insert into $ table ($ fields) values ($ values )");
}
}
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Int $ execrow whether to enable execution and return the number of entries
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, $ execrow, $ table, $ set, $ sqlwhere = ""){
Global $ pdo;
If ($ debug ){
Echo "update $ table set $ set where 1 = 1 $ sqlwhere ";
Exit;
} Elseif ($ execrow ){
Return $ pdo-> exec ("update $ table set $ set where 1 = 1 $ sqlwhere ");
} Else {
$ Pdo-> query ("update $ table set $ set where 1 = 1 $ sqlwhere ");
}
}
/*
Parameter description
Whether debugging is enabled for int $ debug. if debugging is enabled, an SQL statement is output.
Whether int $ execrow enables the number of returned execution entries
String $ table database table
String $ sqlwhere deletion condition, which can be null
*/
Function hrDelete ($ debug, $ execrow, $ table, $ sqlwhere = ""){
Global $ pdo;
If ($ debug ){
Echo "delete from $ table where 1 = 1 $ sqlwhere ";
Exit;
} Elseif ($ execrow ){
Return $ pdo-> exec ("delete from $ table where 1 = 1 $ sqlwhere ");
} Else {
$ Pdo-> query ("delete from $ table where 1 = 1 $ sqlwhere ");
}
}
?>

The parameter comments are clearly written. if you need them, you can directly ask me how to use them.

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.