Home > Backend Development > PHP Tutorial > A php pdo mysql operation class

A php pdo mysql operation class

WBOY
Release: 2016-07-25 08:54:31
Original
973 people have browsed it
  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. * Query a SQL statement
  46. * @param string $sql
  47. * @param array $parameters Parameters that need to be bound
  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. * Insert a piece of data
  64. * @param string $sql
  65. * @param array $parameters
  66. * @return int 1 or 0 Return the number of affected rows
  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. * Update a piece of data
  76. * @param string $sql
  77. * @param array $parameters
  78. * @return int 1 or 0 Return the number of affected rows
  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. * Delete a piece of data
  88. * @param string $sql
  89. * @param array $parameters
  90. * @return int 1 or 0 Return the number of affected rows
  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



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template