Maison  >  Article  >  développement back-end  >  PHP的PDO数据库操作类

PHP的PDO数据库操作类

WBOY
WBOYoriginal
2016-07-25 08:42:001018parcourir

一个简单的PDO类封装。。仅供学习交流

PdoDb 数据库类

  1. /**
  2. * @throws Error
  3. * PDO数据库
  4. */
  5. class PdoDb extends DatabaseAbstract
  6. {
  7. /**
  8. * PDO实例
  9. * @var PDO
  10. */
  11. protected $DB;
  12. /**
  13. * PDO准备语句
  14. * @var PDOStatement
  15. */
  16. protected $Stmt;
  17. /**
  18. * 最后的SQL语句
  19. * @var string
  20. */
  21. protected $Sql;
  22. /**
  23. * 配置信息 $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
  24. * @var array
  25. */
  26. protected $Config;
  27. /**
  28. * 构造函数
  29. * @param array $config
  30. */
  31. public function __construct($config)
  32. {
  33. $this->Config = $config;
  34. }
  35. /**
  36. * 连接数据库
  37. * @return void
  38. */
  39. public function connect()
  40. {
  41. $this->DB = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password'], $this->Config['option']);
  42. //默认把结果序列化成stdClass
  43. $this->DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  44. //自己写代码捕获Exception
  45. $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  46. }
  47. /**
  48. * 断开连接
  49. * @return void
  50. */
  51. public function disConnect()
  52. {
  53. $this->DB = null;
  54. $this->Stmt = null;
  55. }
  56. /**
  57. * 执行sql,返回新加入的id
  58. * @param string $statement
  59. * @return string
  60. */
  61. public function exec($statement)
  62. {
  63. if ($this->DB->exec($statement)) {
  64. $this->Sql = $statement;
  65. return $this->lastId();
  66. }
  67. $this->errorMessage();
  68. }
  69. /**
  70. * 查询sql
  71. * @param string $statement
  72. * @return PdoDb
  73. */
  74. public function query($statement)
  75. {
  76. $res = $this->DB->query($statement);
  77. if ($res) {
  78. $this->Stmt = $res;
  79. $this->Sql = $statement;
  80. return $this;
  81. }
  82. $this->errorMessage();
  83. }
  84. /**
  85. * 序列化一次数据
  86. * @return mixed
  87. */
  88. public function fetchOne()
  89. {
  90. return $this->Stmt->fetch();
  91. }
  92. /**
  93. * 序列化所有数据
  94. * @return array
  95. */
  96. public function fetchAll()
  97. {
  98. return $this->Stmt->fetchAll();
  99. }
  100. /**
  101. * 最后添加的id
  102. * @return string
  103. */
  104. public function lastId()
  105. {
  106. return $this->DB->lastInsertId();
  107. }
  108. /**
  109. * 影响的行数
  110. * @return int
  111. */
  112. public function affectRows()
  113. {
  114. return $this->Stmt->rowCount();
  115. }
  116. /**
  117. * 预备语句
  118. * @param string $statement
  119. * @return PdoDb
  120. */
  121. public function prepare($statement)
  122. {
  123. $res = $this->DB->prepare($statement);
  124. if ($res) {
  125. $this->Stmt = $res;
  126. $this->Sql = $statement;
  127. return $this;
  128. }
  129. $this->errorMessage();
  130. }
  131. /**
  132. * 绑定数据
  133. * @param array $array
  134. * @return PdoDb
  135. */
  136. public function bindArray($array)
  137. {
  138. foreach ($array as $k => $v) {
  139. if (is_array($v)) {
  140. //array的有效结构 array('value'=>xxx,'type'=>PDO::PARAM_XXX)
  141. $this->Stmt->bindValue($k + 1, $v['value'], $v['type']);
  142. } else {
  143. $this->Stmt->bindValue($k + 1, $v, PDO::PARAM_STR);
  144. }
  145. }
  146. return $this;
  147. }
  148. /**
  149. * 执行预备语句
  150. * @return bool
  151. */
  152. public function execute()
  153. {
  154. if ($this->Stmt->execute()) {
  155. return true;
  156. }
  157. $this->errorMessage();
  158. }
  159. /**
  160. * 开启事务
  161. * @return bool
  162. */
  163. public function beginTransaction()
  164. {
  165. return $this->DB->beginTransaction();
  166. }
  167. /**
  168. * 执行事务
  169. * @return bool
  170. */
  171. public function commitTransaction()
  172. {
  173. return $this->DB->commit();
  174. }
  175. /**
  176. * 回滚事务
  177. * @return bool
  178. */
  179. public function rollbackTransaction()
  180. {
  181. return $this->DB->rollBack();
  182. }
  183. /**
  184. * 抛出错误
  185. * @throws Error
  186. * @return void
  187. */
  188. public function errorMessage()
  189. {
  190. $msg = $this->DB->errorInfo();
  191. throw new Error('数据库错误:' . $msg[2]);
  192. }
  193. //---------------------
  194. /**
  195. * 单例实例
  196. * @var PdoDb
  197. */
  198. protected static $_instance;
  199. /**
  200. * 默认数据库
  201. * @static
  202. * @param array $config
  203. * @return PdoDb
  204. */
  205. public static function instance($config)
  206. {
  207. if (!self::$_instance instanceof PdoDb) {
  208. self::$_instance = new PdoDb($config);
  209. self::$_instance->connect();
  210. }
  211. return self::$_instance;
  212. }
  213. //----------------------
  214. /**
  215. * 获取PDO支持的数据库
  216. * @static
  217. * @return array
  218. */
  219. public static function getSupportDriver(){
  220. return PDO::getAvailableDrivers();
  221. }
  222. /**
  223. * 获取数据库的版本信息
  224. * @return array
  225. */
  226. public function getDriverVersion(){
  227. $name = $this->DB->getAttribute(PDO::ATTR_DRIVER_NAME);
  228. return array($name=>$this->DB->getAttribute(PDO::ATTR_CLIENT_VERSION));
  229. }
  230. }
复制代码

使用的时候

  1. PdoDb::instance($config);
复制代码
PHP, PDO


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn