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

yipeiwu_com4年前PHP代码库
百度之后决定使用PDO,至于为什么选择PDO,这里就不再多说,大家自己去百度下就能明白。
既然要换,那最基本就需要有个常用的数据库操作类,也就是所谓的增删改查等,昨晚捣腾了一晚,大致弄出了个雏形,以下就是代码,希望大家能给出点意见。
复制代码 代码如下:

<?php
/*
作者:胡睿
日期:2011/03/19
电邮:hooray0905@foxmail.com
20110319
常用数据库操作,如:增删改查,获取单条记录、多条记录,返回最新一条插入记录id,返回操作记录行数等
*/
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $getcount 是否记数,返回值为行数
int $getrow 是否返回值单条记录
string $table 数据库表
string $fields 需要查询的数据库字段,允许为空,默认为查找全部
string $sqlwhere 查询条件,允许为空
string $orderby 排序,允许为空,默认为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 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();
}
}
}
/*
参数说明
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");
}
}
?>

参数的注释都写的很清楚,如果有人需要,不清楚使用方法可以直接问我。

相关文章

逐步提升php框架的性能

一、当前框架存在什么问题      目前主流的框架Zend Framework、Cakephp等都采用了MVC模式,同时实现了...

php class中public,private,protected的区别以及实例分析

一,public,private,protected的区别public:权限是最大的,可以内部调用,实例调用等。protected: 受保护类型,用于本类和继承类调用。private:...

thinkphp使用phpmailer发送邮件的方法

本文实例讲述了thinkphp使用phpmailer发送邮件的方法。分享给大家供大家参考。具体分析如下: phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎...

PHP自定session保存路径及删除、注销与写入的方法

本文实例讲述了PHP自定session保存路径及删除、注销与写入的方法。分享给大家供大家参考。具体方法如下: 复制代码 代码如下: $sessionpath=session_save_p...

PHP实现网站应用微信登录功能详解

PHP实现网站应用微信登录功能详解

本文实例讲述了PHP实现网站应用微信登录功能。分享给大家供大家参考,具体如下: 背景 近期进行 PC 端的网站开发,需要用到微信授权登录,考虑到前期手机端已经获得了大量的微信用户...