当前位置:Gxlcms > PHP教程 > php实现的pdo公共类定义与用法示例

php实现的pdo公共类定义与用法示例

时间:2021-07-01 10:21:17 帮助过:28人阅读

本文实例讲述了php实现的pdo公共类定义与用法。分享给大家供大家参考,具体如下:

db.class.php :

  1. <?php
  2. class db extends \PDO {
  3. private static $_instance = null;
  4. protected $dbName = '';
  5. protected $dsn;
  6. protected $dbh;
  7. public function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset='utf8') {
  8. try {
  9. $this->dsn = 'mysql:host=' . $dbHost . ';dbname=' . $dbName;
  10. $this->dbh = new \PDO($this->dsn, $dbUser, $dbPasswd);
  11. $this->dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
  12. $this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  13. $this->dbh->exec('SET character_set_connection='.$dbCharset.';SET character_set_client='.$dbCharset.';SET character_set_results='.$dbCharset);
  14. } catch (Exception $e) {
  15. $this->outputError($e->getMessage());
  16. }
  17. }
  18. public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset='utf8') {
  19. if (self::$_instance === null) {
  20. self::$_instance = new self($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
  21. }
  22. return self::$_instance;
  23. }
  24. public function fetchAll($sql, $params = array()) {
  25. try {
  26. $stm = $this->dbh->prepare($sql);
  27. if ($stm && $stm->execute($params)) {
  28. return $stm->fetchAll(\PDO::FETCH_ASSOC);
  29. }
  30. } catch (Exception $e) {
  31. $this->outputError($e->getMessage());
  32. }
  33. }
  34. public function fetchOne($sql, $params = array()) {
  35. try {
  36. $result = false;
  37. $stm = $this->dbh->prepare($sql);
  38. if ($stm && $stm->execute($params)) {
  39. $result = $stm->fetch(\PDO::FETCH_ASSOC);
  40. }
  41. return $result;
  42. } catch (Exception $e) {
  43. $this->outputError($e->getMessage());
  44. }
  45. }
  46. public function fetchColumn($sql, $params = array()) {
  47. $result = '';
  48. try {
  49. $stm = $this->dbh->prepare($sql);
  50. if ($stm && $stm->execute($params)) {
  51. $result = $stm->fetchColumn();
  52. }
  53. return $result;
  54. } catch (Exception $e) {
  55. $this->outputError($e->getMessage());
  56. }
  57. }
  58. public function insert($table, $params = array(), $returnLastId = true) {
  59. $_implode_field = '';
  60. $fields = array_keys($params);
  61. $_implode_field = implode(',', $fields);
  62. $_implode_value = '';
  63. foreach ($fields as $value) {
  64. $_implode_value .= ':'. $value.',';
  65. }
  66. $_implode_value = trim($_implode_value, ',');
  67. $sql = 'INSERT INTO ' . $table . '(' . $_implode_field . ') VALUES ('.$_implode_value.')';
  68. try {
  69. $stm = $this->dbh->prepare($sql);
  70. $result = $stm->execute($params);
  71. if ( $returnLastId ) {
  72. $result = $this->dbh->lastInsertId();
  73. }
  74. return $result;
  75. } catch (Exception $e) {
  76. $this->outputError($e->getMessage());
  77. }
  78. }
  79. public function update($table, $params = array(), $where = null) {
  80. $_implode_field = '';
  81. $_implode_field_arr = array();
  82. if ( empty($where) ) {
  83. return false;
  84. }
  85. $fields = array_keys($params);
  86. foreach ($fields as $key) {
  87. $_implode_field_arr[] = $key . '=' . ':'.$key;
  88. }
  89. $_implode_field = implode(',', $_implode_field_arr);
  90. $sql = 'UPDATE ' . $table . ' SET ' . $_implode_field . ' WHERE ' . $where;
  91. try {
  92. $stm = $this->dbh->prepare($sql);
  93. $result = $stm->execute($params);
  94. return $result;
  95. } catch (Exception $e) {
  96. $this->outputError($e->getMessage());
  97. }
  98. }
  99. public function delete($sql, $params = array()) {
  100. try {
  101. $stm = $this->dbh->prepare($sql);
  102. $result = $stm->execute($params);
  103. return $result;
  104. } catch (Exception $e) {
  105. $this->outputError($e->getMessage());
  106. }
  107. }
  108. public function exec($sql, $params = array()) {
  109. try {
  110. $stm = $this->dbh->prepare($sql);
  111. $result = $stm->execute($params);
  112. return $result;
  113. } catch (Exception $e) {
  114. $this->outputError($e->getMessage());
  115. }
  116. }
  117. private function outputError($strErrMsg) {
  118. throw new Exception("MySQL Error: " . $strErrMsg);
  119. }
  120. public function __destruct() {
  121. $this->dbh = null;
  122. }
  123. }

实例:

  1. <?php
  2. require_once './db.class.php';
  3. $pdo = db::getInstance('127.0.0.1', 'root', '111111', 'php_cms');
  4. $sql = "select id, title1 from cms_wz where id = :id limit 1";
  5. $parame = array('id' => 12,);
  6. $res = $pdo->fetchOne($sql, $parame);
  7. var_dump($res);
  8. $sql = 'SELECT * FROM cms_link';
  9. $result = $db->fetchAll($sql);
  10. print_r($result);
  11. //查询记录数量
  12. $sql = 'SELECT COUNT(*) FROM cms_link';
  13. $count = $db->fetchColumn($sql);
  14. echo $count;
  15. $data = array(
  16. 'siteid' => 1,
  17. 'linktype' => 1,
  18. 'name' => 'google',
  19. 'url' => 'http://www.google.com',
  20. 'listorder' => 0,
  21. 'elite' => 0,
  22. 'passed' => 1,
  23. 'addtime' => time()
  24. );
  25. $lastInsertId = $db->insert('cms_link', $data);
  26. echo $lastInsertId;
  27. //用 try
  28. try {
  29. $result = $pdo->insert('news', $essay);
  30. } catch (Exception $e) {
  31. error_log($e->getMessage());
  32. error_log($e->getMessage() . ' in ' . __FILE__ . ' on line ' . __LINE__);
  33. saveLog('url文章 : ' . $essay['link'] . ' 数据插入失败<br>');
  34. continue;
  35. }
  36. $data = array(
  37. 'siteid' => 1,
  38. 'linktype' => 1,
  39. 'name' => 'google',
  40. 'url' => 'http://www.google.com',
  41. 'listorder' => 0,
  42. 'elite' => 0,
  43. 'passed' => 1,
  44. 'addtime' => time()
  45. );
  46. $db->insert('cms_link', $data);
  47. $sql = 'DELETE FROM cms_link WHERE linkid=4';
  48. $result = $db->delete($sql);
  49. var_dump($result);

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行