PHP encapsulates a database mysql class

Source: Internet
Author: User
PHP encapsulates a database mysql class

  1. // Configure the database
  2. Define ('Db _ host', '127. 0.0.1 '); // server address
  3. Define ('Db _ user', 'root'); // USER name
  4. Define ('Db _ pass', ''); // password
  5. Define ('Db _ DATABASENAME ', 'fenxiao'); // Database
  6. Class Dbmysql
  7. {
  8. /*
  9. * Variable
  10. **/
  11. Private $ tablename = ""; // table name
  12. Private $ fieldname = "*";
  13. Private $ conn;
  14. Private $ where;
  15. Private $ SQL;
  16. Function _ construct ($ tablename)
  17. {
  18. // Generate a connection
  19. $ This-> conn = mysql_connect (DB_HOST, DB_USER, DB_PASS) or die ("connect failed". mysql_error ());
  20. // Select a database
  21. Mysql_select_db (DB_DATABASENAME, $ this-> conn );
  22. // Set the encoding format
  23. Mysql_query ("set names utf8 ");
  24. // Var_dump ($ conn );
  25. $ This-> tablename = $ tablename;
  26. }
  27. // Set SQL statements
  28. Private function setsql ($ SQL)
  29. {
  30. $ This-> SQL = $ SQL;
  31. }
  32. // Set the condition statement
  33. Public function where ($ where)
  34. {
  35. $ This-> where = "where". $ where;
  36. Return $ this;
  37. }
  38. // Press the specified field
  39. Public function field ($ keyword)
  40. {
  41. $ This-> fieldname = $ keyword;
  42. Return $ this;
  43. }
  44. // Set the connection query table
  45. Public function table ($ table1, $ table2, $ field, $ bool)
  46. {
  47. $ This-> tablename = "$ table1 left join $ table2 ON $ table1. $ field $ bool $ table2. $ field ";
  48. // Print_r ($ this-> tablename );
  49. Return $ this;
  50. }
  51. // Configure multi-table queries
  52. Public function addtable ($ table1, $ table2, $ field, $ bool)
  53. {
  54. $ This-> tablename. = "left join $ table2 ON $ table1. $ field $ bool $ table2. $ field ";
  55. // Print_r ($ this-> tablename );
  56. Return $ this;
  57. }
  58. // Set the connection query table
  59. # SELECT * FROM [wx_order left join wx_shopcar ON wx_shopcar.oid = wx_order.oid and wx_order.uid = left join wx_goods ON orders = users] WHERE wx_order.oid = 1 and wx_order.uid = 3
  60. Public function settable ($ SQL)
  61. {
  62. $ This-> tablename = $ SQL;
  63. // Print_r ($ this-> tablename );
  64. Return $ this;
  65. }
  66. // Query all databases for output in array format
  67. Public function select ()
  68. {
  69. /**
  70. * Query all data in the database
  71. **/
  72. $ Arr = array ();
  73. // Execute the SQL statement
  74. $ Result = mysql_query ("select". $ this-> fieldname. "from". $ this-> tablename. $ this-> where, $ this-> conn );
  75. While ($ row = mysql_fetch_assoc ($ result )){
  76. Array_push ($ arr, $ row );
  77. }
  78. Return $ arr;
  79. }
  80. // Search for specified field data
  81. Public function find ()
  82. {
  83. // Execute the SQL statement
  84. $ Result = mysql_query ("select". $ this-> fieldname. "from". $ this-> tablename. $ this-> where, $ this-> conn );
  85. $ Result = mysql_fetch_assoc ($ result );
  86. Return $ result;
  87. }
  88. // Add data to the database
  89. Public function add ($ data)
  90. {
  91. $ Keysql = '';
  92. $ Valuesql = '';
  93. Foreach ($ data as $ key => $ value ){
  94. $ Keysql. = ", '$ key "';
  95. $ Valuesql. = ", '$ value '";
  96. }
  97. $ Keysql = substr ($ keysql, 1 );
  98. $ Valuesql = substr ($ valuesql, 1 );
  99. $ Result = mysql_query ("insert into" '. $ this-> tablename. "' ($ keysql) VALUES ($ valuesql )");
  100. $ Id = mysql_insert_id ();
  101. // Print_r ("insert into" '. $ this-> tablename. "' ($ keysql) VALUES ($ valuesql )");
  102. Return $ id;
  103. }
  104. // Modify the database content
  105. Public function save ($ data)
  106. {
  107. $ Keysql = '';
  108. $ Valuesql = '';
  109. Foreach ($ data as $ key => $ value ){
  110. $ Keysql. = ", '$ key' =' $ value '";
  111. }
  112. $ Keysql = substr ($ keysql, 1 );
  113. // Print_r ($ keysql );
  114. // Echo"
    ";
  115. $ Result = mysql_query ("UPDATE" '. $ this-> tablename. "'set". $ keysql. $ this-> where );
  116. // Print_r ("UPDATE" '. $ this-> tablename. "'set". $ keysql. $ this-> where );
  117. Return $ result;
  118. }
  119. # Delete data
  120. Public function delete ()
  121. {
  122. $ Result = mysql_query ("delete from $ this-> tablename $ this-> where ");
  123. // Print_r ("delete from $ this-> tablename $ this-> where ");
  124. Return $ result;
  125. }
  126. }
  127. /**
  128. * Mysql_fetch_row: returns the fields in a single column [0] => "111"
  129. * Mysql_fetch_field: obtains the field information. [0] => ['name'] => object
  130. * Mysql_fetch_array returns the array data. [0] => "asasds" ['name'] =>
  131. */
  132. ?>


Installed, PHP, 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.