一个基于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实现MySQL更新记录的代码
Jun 07 PHP
PHP JSON 数据解析代码
May 26 PHP
phpMyAdmin 链接表的附加功能尚未激活的问题
Aug 01 PHP
PHP安全技术之 实现php基本安全
Sep 04 PHP
PHP使用ob_start生成html页面的方法
Nov 07 PHP
PHP数组操作类实例
Jul 11 PHP
WordPress的主题编写中获取头部模板和底部模板
Dec 28 PHP
Zend Framework教程之Bootstrap类用法概述
Mar 14 PHP
浅谈php中curl、fsockopen的应用
Dec 10 PHP
PHP实现的获取文件mimes类型工具类示例
Apr 08 PHP
PHP count_chars()函数讲解
Feb 14 PHP
Laravel框架Eloquent ORM修改数据操作示例
Dec 03 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
PL-880隐藏功能
2021/03/01 无线电
adodb与adodb_lite之比较
2006/12/31 PHP
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
2014/05/04 PHP
php一行代码获取文件后缀名实例分析
2014/11/12 PHP
PHP sleep()函数, usleep()函数
2016/08/25 PHP
CSS中一些@规则的用法小结
2021/03/09 HTML / CSS
formValidator3.3的ajaxValidator一些异常分析
2011/07/12 Javascript
使用jQuery Mobile框架开发移动端Web App的入门教程
2016/05/17 Javascript
jQuery基于正则表达式的表单验证功能示例
2017/01/21 Javascript
js 获取图像缩放后的实际宽高,位置等信息
2017/03/07 Javascript
jQuery插件HighCharts实现的2D条状图效果示例【附demo源码下载】
2017/03/15 Javascript
关于jQuery.ajax()的jsonp碰上post详解
2017/07/02 jQuery
jQuery获取table表中的td标签(实例讲解)
2017/07/28 jQuery
React-intl 实现多语言的示例代码
2017/11/03 Javascript
Vue.use源码学习小结
2018/06/20 Javascript
JS实现的RC4加密算法示例
2018/08/16 Javascript
vue组件开发之slider组件使用详解
2020/08/21 Javascript
如何利用node转发请求详解
2020/09/17 Javascript
浅析Python中的join()方法的使用
2015/05/19 Python
深入理解python中函数传递参数是值传递还是引用传递
2017/11/07 Python
pytorch 在sequential中使用view来reshape的例子
2019/08/20 Python
Django框架 查询Extra功能实现解析
2019/09/04 Python
python+selenium+chrome批量文件下载并自动创建文件夹实例
2020/04/27 Python
QML实现钟表效果
2020/06/02 Python
python speech模块的使用方法
2020/09/09 Python
python中time.ctime()实例用法
2021/02/03 Python
在HTML5 canvas里用卷积核进行图像处理的方法
2018/05/02 HTML / CSS
纯html5+css3下拉导航菜单实现代码
2013/03/18 HTML / CSS
百丽国际旗下购物网站:优购
2017/02/28 全球购物
最畅销的视频游戏享受高达90%的折扣:CDKeys
2020/02/10 全球购物
Ajxa常见问题都有哪些
2014/03/26 面试题
2014年党的群众路线整改措施思想汇报
2014/10/12 职场文书
导游词之神仙居景区
2019/11/15 职场文书
python实现Thrift服务端的方法
2021/04/20 Python
windows下快速安装nginx并配置开机自启动的方法
2021/05/11 Servers
Java中的随机数Random
2022/03/17 Java/Android