A class that constructs SQL statements

Source: Internet
Author: User
Tags rtrim strcmp
<无详细内容>
  1. /**
  2. * @package Database Class
  3. * @author Injection (mail:injection.mail@gmail.com)
  4. * @version 1.0
  5. */
  6. @ini_set (' Display_errors ', 0);
  7. Class database{
  8. Private $mDb _host, $mAb _user, $mAb _pwd, $mConn _no;
  9. function DataBase ($Conn _obj) {
  10. $this->connectdb ($Conn _obj);
  11. }
  12. function Connectdb ($Conn _obj) {
  13. $this->mdb_host = $Conn _obj->host;
  14. $this->mad_name = $Conn _obj->user;
  15. $this->mad_pwd = $Conn _obj->pwd;
  16. $this->mconn_no = mysql_connect ($this->mdb_host, $this->mad_name, $this->mad_pwd);
  17. }
  18. function Selectdb ($Conn _obj) {
  19. $this->mdb_name = $Conn _obj->dbname;
  20. mysql_select_db ($this->mdb_name);
  21. }
  22. }
  23. /**
  24. * @package Making Sqls class Exetends Database class
  25. * @author Injection (mail:injection.mail@gmail.com)
  26. * @version 1.0
  27. */
  28. Class Makesql extends database{
  29. Private $MSQL;
  30. function Makesql ($type, $arr _colum_list, $arr _sql_choice) {
  31. $this->makesqltype ($arr _colum_list, $arr _sql_choice);
  32. }
  33. #switch Make List
  34. function Makesqltype ($type, $arr _colum_list, $arr _sql_choice) {
  35. Switch ($type) {
  36. Case ' Insert ':
  37. return $this->makeinsert ($arr _colum_list, $arr _sql_choice);
  38. Case ' SELECT ':
  39. return $this->makeselect ($arr _colum_list, $arr _sql_choice);
  40. Case ' Update ':
  41. return $this->makeupdate ($arr _colum_list, $arr _sql_choice);
  42. Case ' Delete ':
  43. return $this->makedelete ($arr _colum_list, $arr _sql_choice);
  44. }
  45. }
  46. #make Insert
  47. function Makeinsert ($arr _colum_list, $arr _sql_choice) {
  48. $colum _key = Array_keys ($arr _colum_list);
  49. $colum _value = array_values ($arr _colum_list);
  50. $this->msql = "INSERT into". $arr _sql_choice["Tbl_name"]. " (". Join (', ', $colum _key).") VALUES (' ". Join (" ', ' ", $colum _value)." ') ";
  51. return $this->msql;
  52. }
  53. #making Select
  54. function Makeselect ($arr _colum_list = ' * ', $arr _sql_choice) {
  55. $colum _value = Array_keys ($arr _colum_list);
  56. foreach ($arr _sql_choice as $sql _key = $sql _value) {
  57. if (strcmp ($sql _key, ' tbl_name ') = = 0) {
  58. if (strcmp ($arr _colum_list, ' * ')!== 0)
  59. $this->msql = "select". Join (', ', $colum _value). " From ". $sql _value;
  60. Else
  61. $this->msql = "SELECT * from". $sql _value;
  62. }
  63. Else
  64. if (strcmp ($sql _value, ')!== 0)
  65. if (strcmp ($sql _key, ' WHERE ') = = = 0 && strcmp ($sql _value, ' colum ') = = = 0) {
  66. foreach ($arr _colum_list as $colum _key = $colum _value)
  67. $this->msql. = "$colum _key = ' $colum _value ' and";
  68. $this->msql = RTrim ($this->msql, "and");
  69. }
  70. Else
  71. $this->msql. = "$sql _key". $sql _value;
  72. }
  73. return $this->msql;
  74. }
  75. #making Update
  76. function Makeupdate ($arr _colum_list, $arr _sql_choice) {
  77. $this->msql = "UPDATE". $arr _sql_choice[' tbl_name ']. " SET ";
  78. foreach ($arr _colum_list as $colum _key = $colum _value)
  79. $this->msql. = "$colum _key = ' $colum _value ',";
  80. $this->msql = RTrim ($this->msql, ', ');
  81. foreach ($arr _sql_choice as $sql _key = $sql _value) {
  82. if (strcmp ($sql _value, ')!== 0 && strcmp ($sql _key, ' tbl_name ')!==0 && strcmp ($sql _key, ' ORDER by ' )!== 0)
  83. $this->msql. = "$sql _key". $sql _value;
  84. }
  85. return $this->msql;
  86. }
  87. #making Delete
  88. function Makedelete ($arr _colum_list, $arr _sql_choice) {
  89. $this->msql = "DELETE from". $arr _sql_choice[' tbl_name '];
  90. foreach ($arr _sql_choice as $sql _key = $sql _value) {
  91. if (strcmp ($sql _key, ' tbl_name ')!== 0 && strcmp ($sql _value, ')!== 0) {
  92. $this->msql. = "$sql _key". $sql _value;
  93. }
  94. }
  95. return $this->msql;
  96. }
  97. }
Copy Code
  • 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.