PHP的PDO資料庫操作類

來源:互聯網
上載者:User

一個簡單的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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.