一个基于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常用函数 推荐收藏保存
Feb 21 PHP
PHP连接MongoDB示例代码
Sep 06 PHP
PHP 类相关函数的使用详解
May 10 PHP
解析如何去掉CodeIgniter URL中的index.php
Jun 25 PHP
PHP中替换键名的简易方法示例详解
Jan 07 PHP
php实现中文字符截取防乱码方法汇总
Apr 29 PHP
php实现mysql数据库分表分段备份
Jun 18 PHP
PHP实现GIF图片验证码
Nov 04 PHP
PHP实现数组array转换成xml的方法
Jul 19 PHP
PHP编程实现微信企业向用户付款的方法示例
Jul 26 PHP
php json转换相关知识(小结)
Dec 21 PHP
PHP使用Session实现上传进度功能详解
Aug 06 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中计算未知长度的字符串哪个字符出现的次数最多的代码
2012/08/14 PHP
php绘图之在图片上写中文和英文的方法
2015/01/24 PHP
功能强大的PHP发邮件类
2016/08/29 PHP
PHP多维数组指定多字段排序的示例代码
2018/05/16 PHP
枚举JavaScript对象的函数
2006/12/22 Javascript
Javascript miscellanea -display data real time, using window.status
2007/01/09 Javascript
javascipt匹配单行和多行注释的正则表达式
2013/11/20 Javascript
jQuery对JSON数据进行排序输出的方法
2015/06/24 Javascript
jQuery实现的网页竖向菜单效果代码
2015/08/26 Javascript
概述BootStrap中role=&quot;form&quot;及role作用角色
2016/12/08 Javascript
Windows下快速搭建NodeJS本地服务器的步骤
2017/08/09 NodeJs
vue实现仿淘宝结账页面实例代码
2017/11/08 Javascript
vue-cli 默认路由再子路由选中下的选中状态问题及解决代码
2018/09/06 Javascript
Vue 重置组件到初始状态的方法示例
2018/10/10 Javascript
微信小程序使用template标签实现五星评分功能
2018/11/03 Javascript
JS开发自己的类库实例分析
2019/08/28 Javascript
浅析Python基础-流程控制
2016/03/18 Python
Python分支结构(switch)操作简介
2018/01/17 Python
Python爬虫实现百度图片自动下载
2018/02/04 Python
Python 查找字符在字符串中的位置实例
2018/05/02 Python
Python使用requests提交HTTP表单的方法
2018/12/26 Python
详解python配置虚拟环境
2019/04/08 Python
基于Python获取城市近7天天气预报
2019/11/26 Python
Python运行提示缺少模块问题解决方案
2020/04/02 Python
Python使用Excel将数据写入多个sheet
2020/05/16 Python
英国二手iPhone、音乐、电影和游戏商店:musicMagpie
2018/10/26 全球购物
金士达面试非笔试
2012/03/14 面试题
物理专业大学生职业生涯规划书
2014/02/07 职场文书
大学军训感言800字
2014/02/27 职场文书
乡镇消防工作实施方案
2014/03/27 职场文书
公司收款委托书范本
2014/09/20 职场文书
大一新生检讨书
2014/10/29 职场文书
高考作弊检讨书1500字
2015/02/16 职场文书
简历自我评价优缺点
2015/03/11 职场文书
tp5使用layui实现多个图片上传(带附件选择)的方法实例
2021/11/17 PHP
解决xampp安装后Apache无法启动
2022/03/21 Servers