SoFunction
Updated on 2025-03-09

A PDO-based database operation class


<?php
/*
Author: Hu Rui
Date: 2011/03/19
Email: hooray0905@
20110319
Common database operations, such as: adding, deleting, modifying and checking, obtaining a single record, multiple records, returning the latest insert record id, returning the number of rows of operation records, etc.
*/
/*
Parameter description
int $debug Whether to enable debugging, output SQL statements when enabled
int $getcount Whether to count, return value is the number of rows
int $getrow Whether to return a single record of value
string $table database table
string $fields The database field that needs to be queried, is allowed to be empty, default to find all
string $sqlwhere query conditions, allowed to be empty
string $orderby sort, allowed to be empty, default to id inversion order
*/
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
int $debug Whether to enable debugging, output SQL statements when enabled
int $execrow is enabled Return the number of execution entries
int $lastinsertid whether to enable return to the last insertion record id
string $table database table
string $fields fields that need to be inserted into the database
string $values ​​The information that needs to be inserted into the database must correspond one by one to $fields
*/
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 to enable debugging, output SQL statements when enabled
int $execrow Whether to enable 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 the condition, allow empty
*/
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 to enable debugging, output SQL statements when enabled
int $execrow is enabled Return the number of execution entries
string $table database table
string $sqlwhere delete condition, allow empty
*/
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");
}
}
?>