1. <?php
  2. //数据库连接类,不建议直接使用DB,而是对DB封装一层
  3. //这个类不会被污染,不会被直接调用
  4. class DB {
  5. //pdo对象
  6. private $_pdo = null;
  7. //用于存放实例化的对象
  8. static private $_instance = null;
  9.  
  10. //公共静态方法获取实例化的对象
  11. static protected function getInstance() {
  12. if (!(self::$_instance instanceof self)) {
  13. self::$_instance = new self();
  14. }
  15. return self::$_instance;
  16. }
  17.  
  18. //私有克隆
  19. private function __clone() {}
  20.  
  21. //私有构造
  22. private function __construct() {
  23. try {
  24. $this->_pdo = new PDO(DB_DNS, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES '.DB_CHARSET));
  25. $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26. } catch (PDOException $e) {
  27. exit($e->getMessage());
  28. }
  29. }
  30.  
  31. //新增
  32. protected function add($_tables, Array $_addData) {
  33. $_addFields = array();
  34. $_addValues = array();
  35. foreach ($_addData as $_key=>$_value) {
  36. $_addFields[] = $_key;
  37. $_addValues[] = $_value;
  38. }
  39. $_addFields = implode(',', $_addFields);
  40. $_addValues = implode("','", $_addValues);
  41. $_sql = "INSERT INTO $_tables[0] ($_addFields) VALUES ('$_addValues')";
  42. return $this->execute($_sql)->rowCount();
  43. }
  44.  
  45. //修改
  46. protected function update($_tables, Array $_param, Array $_updateData) {
  47. $_where = $_setData = '';
  48. foreach ($_param as $_key=>$_value) {
  49. $_where .= $_value.' AND ';
  50. }
  51. $_where = 'WHERE '.substr($_where, 0, -4);
  52. foreach ($_updateData as $_key=>$_value) {
  53. if (Validate::isArray($_value)) {
  54. $_setData .= "$_key=$_value[0],";
  55. } else {
  56. $_setData .= "$_key='$_value',";
  57. }
  58. }
  59. $_setData = substr($_setData, 0, -1);
  60. $_sql = "UPDATE $_tables[0] SET $_setData $_where";
  61. return $this->execute($_sql)->rowCount();
  62. }
  63.  
  64. //验证一条数据
  65. protected function isOne($_tables, Array $_param) {
  66. $_where = '';
  67. foreach ($_param as $_key=>$_value) {
  68. $_where .=$_value.' AND ';
  69. }
  70. $_where = 'WHERE '.substr($_where, 0, -4);
  71. $_sql = "SELECT id FROM $_tables[0] $_where LIMIT 1";
  72. return $this->execute($_sql)->rowCount();
  73. }
  74.  
  75. //删除
  76. protected function delete($_tables, Array $_param) {
  77. $_where = '';
  78. foreach ($_param as $_key=>$_value) {
  79. $_where .= $_value.' AND ';
  80. }
  81. $_where = 'WHERE '.substr($_where, 0, -4);
  82. $_sql = "DELETE FROM $_tables[0] $_where LIMIT 1";
  83. return $this->execute($_sql)->rowCount();
  84. }
  85.  
  86. //查询
  87. protected function select($_tables, Array $_fileld, Array $_param = array()) {
  88. $_limit = $_order = $_where = $_like = '';
  89. if (Validate::isArray($_param) && !Validate::isNullArray($_param)) {
  90. $_limit = isset($_param['limit']) ? 'LIMIT '.$_param['limit'] : '';
  91. $_order = isset($_param['order']) ? 'ORDER BY '.$_param['order'] : '';
  92. if (isset($_param['where'])) {
  93. foreach ($_param['where'] as $_key=>$_value) {
  94. $_where .= $_value.' AND ';
  95. }
  96. $_where = 'WHERE '.substr($_where, 0, -4);
  97. }
  98. if (isset($_param['like'])) {
  99. foreach ($_param['like'] as $_key=>$_value) {
  100. $_like = "WHERE $_key LIKE '%$_value%'";
  101. }
  102. }
  103. }
  104. $_selectFields = implode(',', $_fileld);
  105. $_table = isset($_tables[1]) ? $_tables[0].','.$_tables[1] : $_tables[0];
  106. $_sql = "SELECT $_selectFields FROM $_table $_where $_like $_order $_limit";
  107. $_stmt = $this->execute($_sql);
  108. $_result = array();
  109. while (!!$_objs = $_stmt->fetchObject()) {
  110. $_result[] = $_objs;
  111. }
  112. return Tool::setHtmlString($_result);
  113. }
  114.  
  115. //总记录
  116. protected function total($_tables, Array $_param = array()) {
  117. $_where = '';
  118. if (isset($_param['where'])) {
  119. foreach ($_param['where'] as $_key=>$_value) {
  120. $_where .= $_value.' AND ';
  121. }
  122. $_where = 'WHERE '.substr($_where, 0, -4);
  123. }
  124. $_sql = "SELECT COUNT(*) as count FROM $_tables[0] $_where";
  125. $_stmt = $this->execute($_sql);
  126. return $_stmt->fetchObject()->count;
  127. }
  128.  
  129. //得到下一个ID
  130. protected function nextId($_tables) {
  131. $_sql = "SHOW TABLE STATUS LIKE '$_tables[0]'";
  132. $_stmt = $this->execute($_sql);
  133. return $_stmt->fetchObject()->Auto_increment;
  134. }
  135.  
  136. //执行SQL
  137. private function execute($_sql) {
  138. try {
  139. $_stmt = $this->_pdo->prepare($_sql);
  140. $_stmt->execute();
  141. } catch (PDOException $e) {
  142. exit('SQL语句:'.$_sql.'<br />错误信息:'.$e->getMessage());
  143. }
  144. return $_stmt;
  145. }
  146. }
  147. ?>

PHP PDO类的更多相关文章

  1. 一个PDO类

    下面是在网上借鉴的一个PDO类: <?php class Database{ private $host = DB_HOST; private $user = DB_USER; private ...

  2. PHP基于单例模式编写PDO类的方法

    一.单例模式简介 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 二.为什么要使用PHP单例模式? 1.php的应用主要在于数据库应用, 所以一个应用中会存在 ...

  3. php-验证码类-PDO类-缩略图类

    Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...

  4. 封装好的PDO类

    封装PDO类,方便使用: <?php header('content-type:text/html;charset=utf-8'); /** * 封装PDODB类 */ // 加载接口 // i ...

  5. PHP PDO类 单例

    <?php /*//pdo连接信息 $pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root ...

  6. pdo类的使用

    使用方法 2.php <?php require_once "./mypdo.php"; $pdo = DAOPDO::getInstance('localhost', 'r ...

  7. 学习到目前,自己封装的db类和pdo类

    DB封装类 <?php class DBDA { public $host = "localhost"; public $uid = "root"; pu ...

  8. PDO和PDOStatement类常用方法

    PDO — PDO 类 PDO::beginTransaction — 启动一个事务 PDO::commit — 提交一个事务 PDO::__construct — 创建一个表示数据库连接的 PDO ...

  9. PHP PDO 使用类

    PDO类 <?php class MYPDO { protected static $_instance = null; protected $dbName = ''; protected $d ...

随机推荐

  1. mybatis中parameterType可以写的别名

    mybatis中parameterType可以写的别名 https://blog.csdn.net/sdzhangshulong/article/details/51749807 _byte byte ...

  2. UIColor延伸:判断两个颜色是否相等

    不管UIColor使用CIColor,CGColor还是其他方式初始化的,其CGColor属性都是可用的.CoreGraphics中提供一个函数,用于判断两个CGColor是否相等,因此我们可以通过这 ...

  3. LruCache:从网络加载图片缓存实例

    OOM异常 堆内存用于存储实例对象,当程序不断创建对象,并且对象都有引用指向,那么垃圾回收机制就不会清理这些对象,当对象多到挤满堆内存的上限后,就产生OOM异常.Android系统为每个应用程序使用的 ...

  4. centos7-每天定时备份 mysql数据库

    centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...

  5. [Coding Practice] Maximum number of zeros in NxN matrix

    Question: Input is a NxN matrix which contains only 0′s and 1′s. The condition is no 1 will occur in ...

  6. Maven命令创建java项目

    ------------------------------java项目搭建--------------------------- 使用Maven构建一个简单的Java项目 1.进入命令行,执行下面的 ...

  7. centos6.8安装并配置zimbra

    一.对域名设置MX记录 二.安装准备 1.关闭selinux vi /etc/selinux/config SELINUX=disabled 2.iptables防火墙端口设置 # iptables ...

  8. Linux查看进程的所有子进程和线程

    得到进程的pid: ps -ef | grep process_name | grep -v "grep" | awk '{print $2}' 查看进程的所有线程 # ps mp ...

  9. 【zoj3645】高斯消元求解普通线性方程

    题意: 给你一个方程组(含有12个方程),求(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11) 方程组的形式是一个二次方程组 (ai1-x1)^2 + (ai2-x2)^2 +( ...

  10. 基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址   //转载请注明出处--本文永久链接:http://www.cnblogs.com/ ...