一个基于PDO的数据库操作类(新) + 一个PDO事务实例
原先已经写过一个PDO的数据库操作类,这次只是在原先基础上进行修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | <?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 ; } } ?> |
另外一段代码是基于我这个数据库操作类的事务实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* 注意,数据库操作表类型必须为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(); } |
代码下载:点击下载
新博客地址
hooray.github.io
hooray.github.io
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架