一个基于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 相关文章推荐
PHP安全配置
Dec 06 PHP
在PHP中读取和写入WORD文档的代码
Apr 09 PHP
PHP获取表单textarea数据中的换行问题
Sep 10 PHP
PHP中防止SQL注入实现代码
Feb 19 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
Jun 21 PHP
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
May 08 PHP
使用PHP接受文件并获得其后缀名的方法
Aug 05 PHP
浅析php如何实现App常用的秒发功能
Aug 03 PHP
PHP版微信第三方实现一键登录及获取用户信息的方法
Oct 14 PHP
PHP XML和数组互相转换详解
Oct 26 PHP
PHP简单验证码功能机制实例详解
Mar 27 PHP
PHP7 整型处理机制修改
Mar 09 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
PHP syntax error, unexpected $end 错误的一种原因及解决
2008/10/25 PHP
基于curl数据采集之单页面采集函数get_html的使用
2013/04/28 PHP
php定界符
2014/06/19 PHP
php app支付宝回调(异步通知)详解
2018/07/25 PHP
改进:论坛UBB代码自动插入方式
2006/12/22 Javascript
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
2007/06/29 Javascript
js 多浏览器分别判断代码
2010/04/01 Javascript
JavaScript 高级篇之函数 (四)
2012/04/07 Javascript
JSONP跨域的原理解析及其实现介绍
2014/03/22 Javascript
node.js操作mysql(增删改查)
2015/07/24 Javascript
整理Javascript流程控制语句学习笔记
2015/11/29 Javascript
js如何改变文章的字体大小
2016/01/08 Javascript
js判断鼠标位置是否在某个div中的方法
2016/02/26 Javascript
如何解决手机浏览器页面点击不跳转浏览器双击放大网页
2016/07/01 Javascript
js中判断变量类型函数typeof的用法总结
2016/08/09 Javascript
js实现网页定位导航功能
2017/03/07 Javascript
jQuery插件FusionCharts绘制的3D环饼图效果示例【附demo源码】
2017/04/02 jQuery
详解如何让Express支持async/await
2017/10/09 Javascript
Bootstrap table表格初始化表格数据的方法
2018/07/25 Javascript
Vuex模块化应用实践示例
2020/02/03 Javascript
[58:57]2018DOTA2亚洲邀请赛3月29日小组赛B组 Effect VS VGJ.T
2018/03/30 DOTA
[43:03]完美世界DOTA2联赛PWL S2 PXG vs Magma 第二场 11.21
2020/11/24 DOTA
python单链表实现代码实例
2013/11/21 Python
利用python获取当前日期前后N天或N月日期的方法示例
2017/07/30 Python
Python实现多进程共享数据的方法分析
2017/12/04 Python
pytorch实现mnist数据集的图像可视化及保存
2020/01/14 Python
Python for循环通过序列索引迭代过程解析
2020/02/07 Python
浅谈Python 参数与变量
2020/06/20 Python
利用css3 translate完美实现表头固定效果
2017/02/28 HTML / CSS
如何在Canvas上的图形/图像绑定事件监听的实现
2020/09/16 HTML / CSS
Java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
2012/05/30 面试题
加拿大留学自荐信
2014/01/28 职场文书
2015年公共机构节能宣传周活动总结
2015/03/26 职场文书
Python中 range | np.arange | np.linspace三者的区别
2022/03/22 Python
python神经网络ResNet50模型
2022/05/06 Python
TS 类型收窄教程示例详解
2022/09/23 Javascript