PHP encapsulates a DB database MySQL class

Source: Internet
Author: User
  1. Configuration 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 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. Setting Up SQL statements
  28. Private Function SetSQL ($sql)
  29. {
  30. $this->sql= $sql;
  31. }
  32. Set conditional statements
  33. Public function where ($where)
  34. {
  35. $this->where= "where". $where;
  36. return $this;
  37. }
  38. by specified field
  39. Public function field ($keyword)
  40. {
  41. $this->fieldname= $keyword;
  42. return $this;
  43. }
  44. Set up a connection query table
  45. Public Function table ($table 1, $table 2, $field, $bool)
  46. {
  47. $this->tablename= "$table 1 left joins $table 2 on $table 1. $field $bool$table2. $field";
  48. Print_r ($this->tablename);
  49. return $this;
  50. }
  51. Set up multiple table queries
  52. Public Function addtable ($table 1, $table 2, $field, $bool)
  53. {
  54. $this->tablename.= "left JOIN $table 2 on $table 1. $field $bool$table2. $field";
  55. Print_r ($this->tablename);
  56. return $this;
  57. }
  58. Set up a connection query table
  59. # #SELECT * from ' Wx_order left join Wx_shopcar on Wx_shopcar.oid=wx_order.oid and Wx_order.uid=wx_shopcar.uid left join WX _goods on Wx_shopcar.gid=wx_goods.gid "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 output as an array
  67. Public Function Select ()
  68. {
  69. /**
  70. * Query all the data in the database
  71. **/
  72. $arr =array ();
  73. Execute 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 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. Adding 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 contents of a database
  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. # #删除数据
  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 of the single column [0]=> "111"
  129. * Mysql_fetch_field: Gets field information. [0]=> [' Name ']=> object
  130. * Mysql_fetch_array returns array data. [0]=> "Asasds" [' Name ']=>
  131. */
  132. ?>
Copy Code
Loaded, PHP, MySQL
  • Related Article

    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.