Home  >  Article  >  Backend Development  >  PHP PDO database operation class

PHP PDO database operation class

WBOY
WBOYOriginal
2016-07-25 08:42:001018browse

A simple PDO class package. . For learning and communication only

PdoDb database class

  1. /**
  2. * @throws Error
  3. * PDO database
  4. */
  5. class PdoDb extends DatabaseAbstract
  6. {
  7. /**
  8. * PDO instance
  9. * @var PDO
  10. */
  11. protected $DB;
  12. /**
  13. * PDO prepared statement
  14. * @var PDOStatement
  15. */
  16. protected $Stmt;
  17. /**
  18. * Last SQL statement
  19. * @var string
  20. */
  21. protected $Sql;
  22. /**
  23. * Configuration information $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
  24. * @var array
  25. */
  26. protected $Config;
  27. /**
  28. * Constructor
  29. * @param array $config
  30. */
  31. public function __construct($config)
  32. {
  33. $this->Config = $config;
  34. }
  35. /**
  36. * Connect to database
  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. * Disconnect
  49. * @return void
  50. */
  51. public function disConnect()
  52. {
  53. $this->DB = null;
  54. $this->Stmt = null;
  55. }
  56. /**
  57. * Execute sql and return the newly added 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. * Query 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. * Serialize data once
  86. * @return mixed
  87. */
  88. public function fetchOne()
  89. {
  90. return $this->Stmt->fetch();
  91. }
  92. /**
  93. * Serialize all data
  94. * @return array
  95. */
  96. public function fetchAll()
  97. {
  98. return $this->Stmt->fetchAll();
  99. }
  100. /**
  101. * Last added id
  102. * @return string
  103. */
  104. public function lastId()
  105. {
  106. return $this->DB->lastInsertId();
  107. }
  108. /**
  109. * Number of rows affected
  110. * @return int
  111. */
  112. public function affectRows()
  113. {
  114. return $this->Stmt->rowCount();
  115. }
  116. /**
  117. * Prepared statement
  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. * Bind data
  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. * Execute prepared statements
  150. * @return bool
  151. */
  152. public function execute()
  153. {
  154. if ($this->Stmt->execute()) {
  155. return true;
  156. }
  157. $this->errorMessage();
  158. }
  159. /**
  160. * Start transaction
  161. * @return bool
  162. */
  163. public function beginTransaction()
  164. {
  165. return $this->DB->beginTransaction();
  166. }
  167. /**
  168. *Execute transaction
  169. * @return bool
  170. */
  171. public function commitTransaction()
  172. {
  173. return $this->DB->commit();
  174. }
  175. /**
  176. * Rollback transaction
  177. * @return bool
  178. */
  179. public function rollbackTransaction()
  180. {
  181. return $this->DB->rollBack();
  182. }
  183. /**
  184. * Throws Error
  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. * Singleton instance
  196. * @var PdoDb
  197. */
  198. protected static $_instance;
  199. /**
  200. * Default database
  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. * Get the database supported by PDO
  216. * @static
  217. * @return array
  218. */
  219. public static function getSupportDriver(){
  220. return PDO::getAvailableDrivers();
  221. }
  222. /**
  223. * Get the version information of the database
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn