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


Posted in PHP onMarch 24, 2011

百度之后决定使用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 相关文章推荐
windows下升级PHP到5.3.3的过程及注意事项
Oct 12 PHP
在PHP中利用wsdl创建标准webservice的实现代码
Dec 07 PHP
微博短链接算法php版本实现代码
Sep 15 PHP
深入PHP FTP类的详解
Jun 13 PHP
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
Jun 25 PHP
如何利用PHP执行.SQL文件
Jul 05 PHP
php mysql_real_escape_string函数用法与实例教程
Sep 30 PHP
php三元运算符知识汇总
Jul 02 PHP
php无序树实现方法
Jul 28 PHP
PHP爬虫之百万级别知乎用户数据爬取与分析
Jan 22 PHP
yii2中LinkPager增加总页数和总记录数的实例
Aug 28 PHP
PHP设计模式之简单工厂和工厂模式实例分析
Mar 25 PHP
Zend Studio (eclipse)使用速度优化方法
Mar 23 #PHP
常见的PHP五种设计模式小结
Mar 23 #PHP
PHP中MVC模式的模板引擎开发经验分享
Mar 23 #PHP
PHP面向接口编程 耦合设计模式 简单范例
Mar 23 #PHP
PHP中用接口、抽象类、普通基类实现“面向接口编程”与“耦合方法”简述
Mar 23 #PHP
php中取得URL的根域名的代码
Mar 23 #PHP
PHP+JS+rsa数据加密传输实现代码
Mar 23 #PHP
You might like
解析yahoo邮件用phpmailer发送的实例
2013/06/24 PHP
php中current、next与reset函数用法实例
2014/11/17 PHP
ZendFramework2连接数据库操作实例
2017/04/18 PHP
php+ajax实现仿百度查询下拉内容功能示例
2017/10/20 PHP
使用Rancher在K8S上部署高性能PHP应用程序的教程
2020/07/10 PHP
在JavaScript中获取请求的URL参数[正则]
2010/12/25 Javascript
jquery属性过滤选择器使用示例
2013/06/18 Javascript
jQuery内置的AJAX功能和JSON的使用实例
2014/07/27 Javascript
省市联动效果的简单实现代码(推荐)
2016/06/06 Javascript
jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
2016/06/28 Javascript
jQuery easyui刷新当前tabs的方法
2016/09/23 Javascript
Ajax的概述与实现过程
2016/11/18 Javascript
微信小程序 Button 组件详解及简单实例
2017/01/10 Javascript
javascript 中的try catch应用总结
2017/04/01 Javascript
详解node如何让一个端口同时支持https与http
2017/07/04 Javascript
深入理解ES6 Promise 扩展always方法
2017/09/26 Javascript
jquery如何实现点击空白处隐藏元素
2017/12/05 jQuery
vue轻量级框架无法获取到vue对象解决方法
2019/05/12 Javascript
vue 检测用户上传图片宽高的方法
2020/02/06 Javascript
详解Python中表达式i += x与i = i + x是否等价
2017/02/08 Python
Python可视化mhd格式和raw格式的医学图像并保存的方法
2019/01/24 Python
将Python字符串生成PDF的实例代码详解
2019/05/17 Python
pytorch的batch normalize使用详解
2020/01/15 Python
python中@property的作用和getter setter的解释
2020/12/22 Python
python 利用openpyxl读取Excel表格中指定的行或列教程
2021/02/06 Python
CSS3制作精致的照片墙特效
2016/06/07 HTML / CSS
html5各种页面切换效果和模态对话框用法总结
2014/12/15 HTML / CSS
印尼美容产品购物网站:PerfectBeauty.id
2017/12/01 全球购物
商务英语大学生职业生涯规划书范文
2014/01/01 职场文书
教师三严三实心得体会
2014/10/11 职场文书
房租涨价通知
2015/04/23 职场文书
公司回复函格式
2015/07/14 职场文书
MySQL单表千万级数据处理的思路分享
2021/06/05 MySQL
Spring中bean的生命周期之getSingleton方法
2021/06/30 Java/Android
Python中的datetime包与time包包和模块详情
2022/02/28 Python
Spring Boot接口定义和全局异常统一处理
2022/04/20 Java/Android