Maison  >  Article  >  développement back-end  >  php pdo封装类代码(支持事务)

php pdo封装类代码(支持事务)

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


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