A phppdomysql operation class

Source: Internet
Author: User
A phppdomysql operation class

  1. /**

  2. * PDO operations
  3. * Created by PhpStorm.
  4. * User: sumiaowen
  5. * Date: 13-11-28
  6. * Time: PM
  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 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 data entry
  64. * @ Param string $ SQL
  65. * @ Param array $ parameters
  66. * @ Return int 1 or 0 returns 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 returns 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 returns 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. }

Code link: 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.