一个基于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程序中的常见漏洞进行攻击
Oct 09 PHP
mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
Mar 15 PHP
php 信息采集程序代码
Mar 17 PHP
PHP 翻页 实例代码
Aug 07 PHP
PHP 导出数据到淘宝助手CSV的方法分享
Feb 27 PHP
php中一个有意思的日期逻辑处理
Mar 25 PHP
PHP判断表单复选框选中状态完整例子
Jun 24 PHP
php实现获取局域网所有用户的电脑IP和主机名、及mac地址完整实例
Jul 18 PHP
深入理解PHP原理之执行周期分析
Jun 01 PHP
PHP进制转换实例分析(2,8,16,36,64进制至10进制相互转换)
Feb 04 PHP
PHP的PDO错误与错误处理
Jan 27 PHP
PHP精确到毫秒秒杀倒计时实例详解
Mar 14 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以及MYSQL日期比较方法
2012/11/29 PHP
php使用ob_start()实现图片存入变量的方法
2014/11/14 PHP
php curl 获取https请求的2种方法
2015/04/27 PHP
php对文件夹进行相关操作(遍历、计算大小)
2015/11/04 PHP
PHP7标量类型declare用法实例分析
2016/09/26 PHP
PHP实现的redis主从数据库状态检测功能示例
2017/07/20 PHP
Javascript document.referrer判断访客来源网址
2020/05/15 Javascript
JavaScript 读取元素的CSS信息的代码
2010/02/07 Javascript
JavaScript 以对象为索引的关联数组
2010/05/19 Javascript
JQuery跨Iframe选择实现代码
2010/08/19 Javascript
基于Jquery+Ajax+Json的高效分页实现代码
2011/10/29 Javascript
jquery text(),val(),html()方法区别总结
2013/11/04 Javascript
JS、CSS加载中的小问题探讨
2013/11/26 Javascript
Nodejs为什么选择javascript为载体语言
2015/01/13 NodeJs
每天一篇javascript学习小结(Boolean对象)
2015/11/12 Javascript
基于jquery实现可定制的web在线富文本编辑器附源码下载
2015/11/17 Javascript
BootStrap的select2既可以查询又可以输入的实现代码
2017/02/17 Javascript
vue中动态绑定表单元素的属性方法
2018/02/23 Javascript
微信小程序与公众号实现数据互通的方法
2019/07/25 Javascript
JavaScript 实现同时选取多个时间段的方法
2019/10/17 Javascript
JS实现简易计算器
2020/02/14 Javascript
vue-video-player 断点续播的实现
2021/02/01 Vue.js
利用Python-iGraph如何绘制贴吧/微博的好友关系图详解
2017/11/02 Python
基于python中pygame模块的Linux下安装过程(详解)
2017/11/09 Python
Python实现针对给定字符串寻找最长非重复子串的方法
2018/04/21 Python
解决python升级引起的pip执行错误的问题
2018/06/12 Python
Python3.5迭代器与生成器用法实例分析
2019/04/30 Python
Keras使用tensorboard显示训练过程的实例
2020/02/15 Python
Django实现内容缓存实例方法
2020/06/30 Python
css3个性化字体_动力节点Java学院整理
2017/07/12 HTML / CSS
Under Armour安德玛意大利官网:美国高端运动科技品牌
2020/01/16 全球购物
百度JavaScript笔试题
2015/01/15 面试题
个人能力自我鉴赏
2014/01/25 职场文书
外贸业务员求职信
2014/06/16 职场文书
成事在人观后感
2015/06/16 职场文书
2019最新版劳务派遣管理制度
2019/08/16 职场文书