职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 503|回复: 3

一个基于PDO的数据库操作类(新) 一个PDO事务实例``

[复制链接]
周周猪猪 发表于 2011-8-8 10:07 | 显示全部楼层 |阅读模式
原先已经写过一个PDO的数据库操作类,这次只是在原先基础上进行修改。


<?php
/*
* 作者:胡睿
* 日期:2011/03/19
* 电邮:hooray0905@foxmail.com
*
* 20110319
* 常用数据库操作,如:增删改查,获取单条记录、多条记录,返回最新一条插入记录id,返回操作记录行数等
* 20110630
* 整体修改方法,合并部分参数
* 规范代码,一个方法里只有1个return语句
*/
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 返回数组
1 返回单条记录
2 返回行数
string $table 数据库表
string $fields 需要查询的数据库字段,允许为空,默认为查找全部
string $sqlwhere 查询条件,允许为空
string $orderby 排序,允许为空,默认为id倒序
*/
function hrSelect($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="id desc"){
global $pdo;
if($debug){
if($mode == 2){
echo "select count(*) from $table where 1=1 $sqlwhere order by $orderby";
}elseif($mode == 1){
echo "select $fields from $table where 1=1 $sqlwhere";
}else{
echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
}
exit;
}else{
if($mode == 2){
$rs = $pdo->query("select count(*) from $table where 1=1 $sqlwhere order by $orderby");
$return = $rs->fetchColumn();
}elseif($mode == 1){
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere");
$return = $rs->fetch();
}else{
$rs = $pdo->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
$return = $rs->fetchAll();
}
return $return;
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 默认insert,无返回信息
1 返回执行条目数
2 返回最后一次插入记录的id
string $table 数据库表
string $fields 需要插入数据库的字段
string $values 需要插入数据库的信息,必须与$fields一一对应
*/
function hrInsert($debug, $mode, $table, $fields, $values){
global $pdo;
if($debug){
echo "insert into $table ($fields) values ($values)";
exit;
}else{
if($mode == 2){
$return = $pdo->lastInsertId("insert into $table ($fields) values ($values)");
}elseif($mode == 1){
$return = $pdo->exec("insert into $table ($fields) values ($values)");
}else{
$pdo->query("insert into $table ($fields) values ($values)");
exit;
}
return $return;
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 默认update,无返回信息
1 返回执行条目数
string $table 数据库表
string $set 需要更新的字段及内容,格式:a='abc',b=2,c='2010-10-10 10:10:10'
string $sqlwhere 修改条件,允许为空
*/
function hrUpdate($debug, $mode, $table, $set, $sqlwhere=""){
global $pdo;
if($debug){
echo "update $table set $set where 1=1 $sqlwhere";
exit;
}else{
if($mode==1){
$return = $pdo->exec("update $table set $set where 1=1 $sqlwhere");
}else{
$pdo->query("update $table set $set where 1=1 $sqlwhere");
exit;
}
return $return;
}
}
/*
参数说明
int $debug 是否开启调试,开启则输出sql语句
int $mode 0 默认delete,无返回信息
1 返回执行条目数
string $table 数据库表
string $sqlwhere 删除条件,允许为空
*/
function hrDelete($debug, $mode, $table, $sqlwhere=""){
global $pdo;
if($debug){
echo "delete from $table where 1=1 $sqlwhere";
exit;
}else{
if($mode == 1){
$return = $pdo->exec("delete from $table where 1=1 $sqlwhere");
}else{
$pdo->query("delete from $table where 1=1 $sqlwhere");
exit;
}
return $return;
}
}
?>

另外一段代码是基于我这个数据库操作类的事务实例:
复制代码 代码如下:
/*
注意,数据库操作表类型必须为InnoDB,其他类型不支持事务
PDO事务机制
$pdo->beginTransaction(); --开启事务
$pdo->commit(); --结束事务
$pdo->rollBack(); --回滚操作

示例,用try/catch包住db操作,当事务内的db操作出现中断,则执行回滚并抛出异常信息。
*/
try{
$pdo->beginTransaction();
hrInsert(0,1,"class","name,parentid","'god',0"); //可以正常执行
hrInsert(0,0,0,"tb_searchlog","userid,code","4"); //出错
$pdo->commit();
}catch(Exception $e){
$pdo->rollBack();
echo "Failed: " . $e->getMessage();
}


能文能武 发表于 2011-8-8 10:43 | 显示全部楼层
原来还有这么多内幕啊,长见识了,呵呵
江波 发表于 2011-8-8 10:43 | 显示全部楼层
最近比较倒霉
Jethro 发表于 2011-8-13 11:17 | 显示全部楼层
真是有你的!
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-28 10:18 , Processed in 0.138216 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表