Use Singleton mode to implement mysql classes

Source: Internet
Author: User
Use Singleton mode to implement mysql classes

  1. Defined ('ACC ') | exit ('Access Denied ');
  2. // Encapsulate the mysql operation class, including the connection and query functions.
  3. Class mysql extends absdb {
  4. Protected static $ ins = null;
  5. Protected $ host; // host name
  6. Protected $ user; // user name
  7. Protected $ passwd; // password
  8. Protected $ db; // database name
  9. Protected $ port; // port
  10. Protected $ conn = null;
  11. // Obtain an object through internal operations
  12. Public static function getIns (){
  13. If (self ::$ ins === null ){
  14. Self: $ ins = new self ();
  15. }
  16. $ Conf = conf: getIns ();
  17. Self: $ ins-> host = $ conf-> host;
  18. Self: $ ins-> user = $ conf-> user;
  19. Self: $ ins-> passwd = $ conf-> pwd;
  20. Self: $ ins-> db = $ conf-> db;
  21. Self: $ ins-> port = $ conf-> port;
  22. Self: $ ins-> connect ();
  23. Self: $ ins-> select_db ();
  24. Self: $ ins-> setChar ();
  25. Return self: $ ins;
  26. }
  27. // Do not allow external users to perform new operations,
  28. Protected function _ construct (){
  29. }
  30. // Connect to the database
  31. Public function connect (){
  32. $ This-> conn = @ mysql_connect ($ this-> host, $ this-> user, $ this-> passwd, $ this-> port );
  33. If (! $ This-> conn ){
  34. $ Error = new Exception ('database not connected', 9 );
  35. Throw $ error;
  36. }
  37. }
  38. // Send an SQL query
  39. Public function query ($ SQL ){
  40. $ Rs = mysql_query ($ SQL, $ this-> conn );
  41. If (! $ Rs ){
  42. Log: write ($ SQL );
  43. }
  44. Return $ rs;
  45. }
  46. // Encapsulate a getAll method
  47. // Parameter: $ SQL
  48. // Return value: array, false
  49. Public function getAll ($ SQL ){
  50. $ Rs = $ this-> query ($ SQL );
  51. If (! $ Rs ){
  52. Return false;
  53. }
  54. $ List = array ();
  55. While ($ row = mysql_fetch_assoc ($ rs )){
  56. $ List [] = $ row;
  57. }
  58. Return $ list;
  59. }
  60. // Encapsulate a getRow method
  61. // Parameter: $ SQL
  62. // Return value: array, false
  63. Public function getRow ($ SQL ){
  64. $ Rs = $ this-> query ($ SQL );
  65. If (! $ Rs ){
  66. Return false;
  67. }
  68. Return mysql_fetch_assoc ($ rs );
  69. }
  70. // Encapsulate a getOne method,
  71. // Parameter: $ SQL
  72. // Return value: int, str (single value)
  73. Public function getOne ($ SQL ){
  74. $ Rs = $ this-> query ($ SQL );
  75. If (! $ Rs ){
  76. Return false;
  77. }
  78. $ Tmp = mysql_fetch_row ($ rs );
  79. Return $ tmp [0];
  80. }
  81. // Encapsulate an afftect_rows () method
  82. // Parameter: None
  83. // Returns the number of affected int rows.
  84. Public function affected_rows (){
  85. Return mysql_affected_rows ($ this-> conn );
  86. }
  87. // Return the value of the newly generated auto_increment column
  88. Public function last_id (){
  89. Return mysql_insert_id ($ this-> conn );
  90. }
  91. // Select the database function
  92. Public function select_db (){
  93. $ SQL = 'use'. $ this-> db;
  94. Return $ this-> query ($ SQL );
  95. }
  96. // Set the character set function
  97. Public function setChar (){
  98. $ SQL = 'set names utf8 ';
  99. Return $ this-> query ($ SQL );
  100. }
  101. // Automatically generate the insert statement, update the statement, and execute
  102. Public function autoExecute ($ data, $ table, $ act = 'insert', $ where = ''){
  103. If ($ act = 'insert '){
  104. $ SQL = 'insert'. $ table .'(';
  105. $ SQL. = implode (',', (array_keys ($ data )));
  106. $ SQL. = ') values (\'';
  107. $ SQL. = implode ("','", array_values ($ data ));
  108. $ SQL. = "')";
  109. } Else if ($ act = 'update '){
  110. If (! Trim ($ where )){
  111. Return false;
  112. }
  113. $ SQL = 'update'. $ table. 'set ';
  114. Foreach ($ data as $ k => $ v ){
  115. $ SQL. = $ k;
  116. $ SQL. = ';
  117. $ SQL. = "'". $ v ."',";
  118. }
  119. $ SQL = substr ($ SQL, 0,-1 );
  120. $ SQL. = 'where ';
  121. $ SQL. = $ where;
  122. } Else {
  123. Return false;
  124. }
  125. // Return $ SQL;
  126. Return $ this-> query ($ SQL );
  127. }
  128. }


Mysql

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.