A PHP PDO MySQL operation class

Source: Internet
Author: User
  1. /**

  2. * PDO operation
  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 an 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 returns the number of rows affected
  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 One piece of data
  76. * @param string $sql
  77. * @param array $parameters
  78. * @return int 1 or 0 returns the number of rows affected
  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 One piece of data
  88. * @param string $sql
  89. * @param array $parameters
  90. * @return int 1 or 0 returns the number of rows affected
  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. }

Copy Code

Code link Address: Https://github.com/sumiaowen/mynotes/tree/master/pdo_mysql_class

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.