您好,欢迎访问一九零五行业门户网

一个基于PDO的数据库操作类

工作一年以来,所做的项目使用的都是adodb,但其的代码臃肿和执行效率低导致现在需要更换。
百度之后决定使用pdo,至于为什么选择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();
}
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $execrow 是否开启返回执行条目数
int $lastinsertid 是否开启返回最后一条插入记录id
string $table 数据库表
string $fields 需要插入数据库的字段
string $values 需要插入数据库的信息,必须与$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));
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $execrow 是否开启执行并返回条目数
string $table 数据库表
string $set 需要更新的字段及内容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
string $sqlwhere 修改条件,允许为空
*/
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);
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $execrow 是否开启返回执行条目数
string $table 数据库表
string $sqlwhere 删除条件,允许为空
*/
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);
}
}
?>
参数的注释都写的很清楚,如果有人需要,不清楚使用方法可以直接问我。
其它类似信息

推荐信息