After Baidu decided to use PDO, as for why choose PDO, here no longer say, we go to Baidu under can understand.
Since to change, that the most basic need to have a common database operation class, that is, the so-called additions and deletions 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 the Code code as follows:
/*
Author: Hu Rui
Date: 2011/03/19
E-mail: hooray0905@foxmail.com
20110319
Commonly used database operations, such as: Add or delete changes, get a single record, multiple records, return the latest one inserted record ID, return operation record number of rows, etc.
*/
/*
Parameter description
int $debug Whether debugging is turned on, output SQL statement is turned on
int $getcount whether to count, the return value is the number of rows
int $getrow whether to return the value of a single record
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, $getcount, $getrow, $table, $fields = "*", $sqlwhere = "", $orderby = "id desc") {
Global $pdo;
if ($debug) {
if ($getcount) {
echo "SELECT COUNT (*) from $table where 1=1 $sqlwhere the 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 the order by $orderby");
return $rs->fetch ();
}else{
$rs = $pdo->query ("Select $fields from $table where 1=1 $sqlwhere the order by $orderby");
return $rs->fetchall ();
}
}
}
/*
Parameter description
int $debug Whether debugging is turned on, output SQL statement is turned on
int $execrow whether to turn on the number of returned execution entries
int $lastinsertid whether to turn on returns the last entry to insert a record ID
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, $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, output SQL statement is turned on
int $execrow whether to turn on execution and return the number of 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, $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, output SQL statement is turned on
int $execrow whether to turn on the number of returned 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 parameters of the comments are written very clearly, if someone needs, not clear how to use the method can ask me directly.
The above describes Parallels Desktop 71 PDO-based database operations classes, including Parallels Desktop 7 aspects of the content, I hope to be interested in PHP tutorial friends helpful.