A _php technique of database operation class based on PDO

Source: Internet
Author: User
After Baidu decided to use PDO, as for why choose PDO, here will no longer say, we go to Baidu under can understand.
Since to change, that most basic need to have a commonly used database operation class, that is, the so-called deletion and change check and so on, last night churn a night, roughly made a prototype, the following is the code, I hope you can give a point of view.
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 to check, get a single record, more than one record, return to the latest Insert record ID, return the number of operations record rows, etc.
*/
/*
Parameter description
int $debug Whether debugging is turned on, then output SQL statement
int $getcount count, return value is number of rows
int $getrow whether to return a single record of the value
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, $getcount, $getrow, $table, $fields = "*", $sqlwhere = "", $orderby = "id desc") {
Global $pdo;
if ($debug) {
if ($getcount) {
echo "SELECT COUNT (*) from the $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
int $debug Whether debugging is turned on, then output SQL statement
int $execrow whether to turn on return number of execution entries
int $lastinsertid Whether to open returns the last Insert record ID
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, $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
int $debug Whether debugging is turned on, then output SQL statement
int $execrow whether to open execution and return the number of 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, $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
int $debug Whether debugging is turned on, then output SQL statement
int $execrow whether to turn on return number of execution entries
String $table database table
String $sqlwhere Delete condition, allow 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 annotation of the parameter is written very clearly, if someone needs, not clear use method can ask me directly.

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.