首页 后端开发 php教程 PDO数据库操作类

PDO数据库操作类

Jul 25, 2016 am 08:44 AM

  1. class HRDB{
  2. protected $pdo;
  3. protected $res;
  4. protected $config;
  5. /*构造函数*/
  6. function __construct($config){
  7. $this->Config = $config;
  8. $this->connect();
  9. }
  10. /*数据库连接*/
  11. public function connect(){
  12. $this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']);
  13. $this->pdo->query('set names utf8;');
  14. //把结果序列化成stdClass
  15. //$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  16. //自己写代码捕获Exception
  17. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18. }
  19. /*数据库关闭*/
  20. public function close(){
  21. $this->pdo = null;
  22. }
  23. public function query($sql){
  24. $res = $this->pdo->query($sql);
  25. if($res){
  26. $this->res = $res;
  27. }
  28. }
  29. public function exec($sql){
  30. $res = $this->pdo->exec($sql);
  31. if($res){
  32. $this->res = $res;
  33. }
  34. }
  35. public function fetchAll(){
  36. return $this->res->fetchAll();
  37. }
  38. public function fetch(){
  39. return $this->res->fetch();
  40. }
  41. public function fetchColumn(){
  42. return $this->res->fetchColumn();
  43. }
  44. public function lastInsertId(){
  45. return $this->res->lastInsertId();
  46. }
  47. /**
  48. * 参数说明
  49. * int $debug 是否开启调试,开启则输出sql语句
  50. * 0 不开启
  51. * 1 开启
  52. * 2 开启并终止程序
  53. * int $mode 返回类型
  54. * 0 返回多条记录
  55. * 1 返回单条记录
  56. * 2 返回行数
  57. * string/array $table 数据库表,两种传值模式
  58. * 普通模式:
  59. * 'tb_member, tb_money'
  60. * 数组模式:
  61. * array('tb_member', 'tb_money')
  62. * string/array $fields 需要查询的数据库字段,允许为空,默认为查找全部,两种传值模式
  63. * 普通模式:
  64. * 'username, password'
  65. * 数组模式:
  66. * array('username', 'password')
  67. * string/array $sqlwhere 查询条件,允许为空,两种传值模式
  68. * 普通模式:
  69. * 'and type = 1 and username like "%os%"'
  70. * 数组模式:
  71. * array('type = 1', 'username like "%os%"')
  72. * string $orderby 排序,默认为id倒序
  73. */
  74. public function select($debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){
  75. //参数处理
  76. if(is_array($table)){
  77. $table = implode(', ', $table);
  78. }
  79. if(is_array($fields)){
  80. $fields = implode(', ', $fields);
  81. }
  82. if(is_array($sqlwhere)){
  83. $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  84. }
  85. //数据库操作
  86. if($debug === 0){
  87. if($mode === 2){
  88. $this->query("select count(tbid) from $table where 1=1 $sqlwhere");
  89. $return = $this->fetchColumn();
  90. }else if($mode === 1){
  91. $this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
  92. $return = $this->fetch();
  93. }else{
  94. $this->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
  95. $return = $this->fetchAll();
  96. }
  97. return $return;
  98. }else{
  99. if($mode === 2){
  100. echo "select count(tbid) from $table where 1=1 $sqlwhere";
  101. }else if($mode === 1){
  102. echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
  103. }
  104. else{
  105. echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
  106. }
  107. if($debug === 2){
  108. exit;
  109. }
  110. }
  111. }
  112. /**
  113. * 参数说明
  114. * int $debug 是否开启调试,开启则输出sql语句
  115. * 0 不开启
  116. * 1 开启
  117. * 2 开启并终止程序
  118. * int $mode 返回类型
  119. * 0 无返回信息
  120. * 1 返回执行条目数
  121. * 2 返回最后一次插入记录的id
  122. * string/array $table 数据库表,两种传值模式
  123. * 普通模式:
  124. * 'tb_member, tb_money'
  125. * 数组模式:
  126. * array('tb_member', 'tb_money')
  127. * string/array $set 需要插入的字段及内容,两种传值模式
  128. * 普通模式:
  129. * 'username = "test", type = 1, dt = now()'
  130. * 数组模式:
  131. * array('username = "test"', 'type = 1', 'dt = now()')
  132. */
  133. public function insert($debug, $mode, $table, $set){
  134. //参数处理
  135. if(is_array($table)){
  136. $table = implode(', ', $table);
  137. }
  138. if(is_array($set)){
  139. $set = implode(', ', $set);
  140. }
  141. //数据库操作
  142. if($debug === 0){
  143. if($mode === 2){
  144. $this->query("insert into $table set $set");
  145. $return = $this->lastInsertId();
  146. }else if($mode === 1){
  147. $this->exec("insert into $table set $set");
  148. $return = $this->res;
  149. }else{
  150. $this->query("insert into $table set $set");
  151. $return = NULL;
  152. }
  153. return $return;
  154. }else{
  155. echo "insert into $table set $set";
  156. if($debug === 2){
  157. exit;
  158. }
  159. }
  160. }
  161. /**
  162. * 参数说明
  163. * int $debug 是否开启调试,开启则输出sql语句
  164. * 0 不开启
  165. * 1 开启
  166. * 2 开启并终止程序
  167. * int $mode 返回类型
  168. * 0 无返回信息
  169. * 1 返回执行条目数
  170. * string $table 数据库表,两种传值模式
  171. * 普通模式:
  172. * 'tb_member, tb_money'
  173. * 数组模式:
  174. * array('tb_member', 'tb_money')
  175. * string/array $set 需要更新的字段及内容,两种传值模式
  176. * 普通模式:
  177. * 'username = "test", type = 1, dt = now()'
  178. * 数组模式:
  179. * array('username = "test"', 'type = 1', 'dt = now()')
  180. * string/array $sqlwhere 修改条件,允许为空,两种传值模式
  181. * 普通模式:
  182. * 'and type = 1 and username like "%os%"'
  183. * 数组模式:
  184. * array('type = 1', 'username like "%os%"')
  185. */
  186. public function update($debug, $mode, $table, $set, $sqlwhere=""){
  187. //参数处理
  188. if(is_array($table)){
  189. $table = implode(', ', $table);
  190. }
  191. if(is_array($set)){
  192. $set = implode(', ', $set);
  193. }
  194. if(is_array($sqlwhere)){
  195. $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  196. }
  197. //数据库操作
  198. if($debug === 0){
  199. if($mode === 1){
  200. $this->exec("update $table set $set where 1=1 $sqlwhere");
  201. $return = $this->res;
  202. }else{
  203. $this->query("update $table set $set where 1=1 $sqlwhere");
  204. $return = NULL;
  205. }
  206. return $return;
  207. }else{
  208. echo "update $table set $set where 1=1 $sqlwhere";
  209. if($debug === 2){
  210. exit;
  211. }
  212. }
  213. }
  214. /**
  215. * 参数说明
  216. * int $debug 是否开启调试,开启则输出sql语句
  217. * 0 不开启
  218. * 1 开启
  219. * 2 开启并终止程序
  220. * int $mode 返回类型
  221. * 0 无返回信息
  222. * 1 返回执行条目数
  223. * string $table 数据库表
  224. * string/array $sqlwhere 删除条件,允许为空,两种传值模式
  225. * 普通模式:
  226. * 'and type = 1 and username like "%os%"'
  227. * 数组模式:
  228. * array('type = 1', 'username like "%os%"')
  229. */
  230. public function delete($debug, $mode, $table, $sqlwhere=""){
  231. //参数处理
  232. if(is_array($sqlwhere)){
  233. $sqlwhere = ' and '.implode(' and ', $sqlwhere);
  234. }
  235. //数据库操作
  236. if($debug === 0){
  237. if($mode === 1){
  238. $this->exec("delete from $table where 1=1 $sqlwhere");
  239. $return = $this->res;
  240. }else{
  241. $this->query("delete from $table where 1=1 $sqlwhere");
  242. $return = NULL;
  243. }
  244. return $return;
  245. }else{
  246. echo "delete from $table where 1=1 $sqlwhere";
  247. if($debug === 2){
  248. exit;
  249. }
  250. }
  251. }
  252. }
复制代码

PDO


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

解释PHP中晚期静态结合的概念。 解释PHP中晚期静态结合的概念。 Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章讨论了框架中的基本安全功能,以防止漏洞,包括输入验证,身份验证和常规更新。

自定义/扩展框架:如何添加自定义功能。 自定义/扩展框架:如何添加自定义功能。 Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

See all articles