Heim  >  Artikel  >  Backend-Entwicklung  >  一个php pdo mysql操作类

一个php pdo mysql操作类

WBOY
WBOYOriginal
2016-07-25 08:54:31929Durchsuche
  1. /**

  2. * PDO 操作
  3. * Created by PhpStorm.
  4. * User: sumiaowen
  5. * Date: 13-11-28
  6. * Time: 下午9:12
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. class Pdo_db
  10. {
  11. private $dns = null;
  12. private $username = null;
  13. private $password = null;
  14. private $conn = null;
  15. private static $_instance = null;
  16. private function __construct($params = array())

  17. {
  18. $this->dns = $params['dns'];
  19. $this->username = $params['username'];
  20. $this->password = $params['password'];
  21. $this->_connect();

  22. }
  23. private function __clone() { }

  24. public function get_instance($params = array())

  25. {
  26. if(!(self::$_instance instanceof self))
  27. {
  28. self::$_instance = new self($params);
  29. }
  30. return self::$_instance;

  31. }
  32. private function _connect()

  33. {
  34. try
  35. {
  36. $this->conn = new PDO($this->dns, $this->username, $this->password);
  37. $this->conn->query('set names utf8');
  38. }
  39. catch(PDOException $e)
  40. {
  41. exit('PDOException: ' . $e->getMessage());
  42. }
  43. }
  44. /**

  45. * 查询一条SQL语句
  46. * @param string $sql
  47. * @param array $parameters 需要绑定的参数
  48. * @param int $option
  49. * @return array
  50. */
  51. public function query($sql, $parameters = array(), $option = PDO::FETCH_ASSOC)
  52. {
  53. $stmt = $this->conn->prepare($sql);
  54. $stmt->execute($parameters);
  55. $tmp = array();

  56. while($row = $stmt->fetch($option))
  57. {
  58. $tmp[] = $row;
  59. }
  60. return $tmp;

  61. }
  62. /**

  63. * 插入一条数据
  64. * @param string $sql
  65. * @param array $parameters
  66. * @return int 1 or 0 返回影响行数
  67. */
  68. public function insert($sql, $parameters = array())
  69. {
  70. $stmt = $this->conn->prepare($sql);
  71. $stmt->execute($parameters);
  72. return $stmt->rowCount();

  73. }
  74. /**

  75. * 更新一条数据
  76. * @param string $sql
  77. * @param array $parameters
  78. * @return int 1 or 0 返回影响行数
  79. */ bbs.it-home.org
  80. public function update($sql, $parameters = array())
  81. {
  82. $stmt = $this->conn->prepare($sql);
  83. $stmt->execute($parameters);
  84. return $stmt->rowCount();

  85. }
  86. /**

  87. * 删除一条数据
  88. * @param string $sql
  89. * @param array $parameters
  90. * @return int 1 or 0 返回影响行数
  91. */
  92. public function delete($sql, $parameters = array())
  93. {
  94. $stmt = $this->conn->prepare($sql);
  95. $stmt->execute($parameters);
  96. return $stmt->rowCount();

  97. }
  98. }
复制代码

代码链接地址:https://github.com/sumiaowen/mynotes/tree/master/pdo_mysql_class



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn