Simple DAO class based on PHP and MySQL

Source: Internet
Author: User
Simpledao.class
  1. Require_once (' firephpcore/firephp.class.php ');
  2. $firephp = Firephp::getinstance (true); Debugger in Firefox
  3. Class Simpledao {
  4. Private $_table = null;
  5. private static $_con = null;
  6. Public Function Simpledao () {
  7. if ($this->_con = = null) {
  8. $this->_con = @mysql_connect ("localhost", "root", "123456");
  9. if ($this->_con = = FALSE) {
  10. Echo ("Connect to DB Server failed.");
  11. $this->_con = null;
  12. Return
  13. }
  14. $firephp->log ("New DAO object");
  15. @mysql_select_db ("Swan", $this->_con);
  16. }
  17. }
  18. Public function table ($tablename) {
  19. $this->_table = $tablename;
  20. return $this;
  21. }
  22. Public Function Query ($sql) {
  23. $result = @mysql_query ($sql);
  24. $ret = [];
  25. if ($result) {
  26. while ($row = Mysql_fetch_array ($result)) {
  27. $ret [] = $row;
  28. }
  29. }
  30. return $ret;
  31. }
  32. Public function Get ($where = null) {
  33. $sql = "SELECT * from". $this->_table;
  34. $sql = $sql. $this->_getwherestring ($where);
  35. echo "[Get]". $sql. "
    ";
  36. return $this->query ($sql);
  37. }
  38. Public Function Insert ($params) {
  39. if ($params = = NULL | |!is_array ($params)) {
  40. return-1;
  41. }
  42. $keys = $this->_getparamkeystring ($params);
  43. $vals = $this->_getparamvalstring ($params);
  44. $sql = "INSERT into". $this->_table. " (". $keys.") VALUES (". $vals.") ";
  45. echo "[insert]". $sql. "
    ";
  46. $result = @mysql_query ($sql);
  47. if (! $result) {
  48. return-1;
  49. }
  50. return @mysql_insert_id ();
  51. }
  52. Public Function Update ($params, $where = null) {
  53. if ($params = = NULL | |!is_array ($params)) {
  54. return-1;
  55. }
  56. $upvals = $this->_getupdatestring ($params);
  57. $wheres = $this->_getwherestring ($where);
  58. $sql = "Update". $this->_table. "Set". $upvals. " ". $wheres;
  59. echo "[UPDATE]". $sql. "
    ";
  60. $result = @mysql_query ($sql);
  61. if (! $result) {
  62. return-1;
  63. }
  64. return @mysql_affected_rows ();
  65. }
  66. Public Function Delete ($where) {
  67. $wheres = $this->_getwherestring ($where);
  68. $sql = "Delete from". $this->_table. $wheres;
  69. echo "[delete]". $sql. "
    ";
  70. $result = @mysql_query ($sql);
  71. if (! $result) {
  72. return-1;
  73. }
  74. return @mysql_affected_rows ();
  75. }
  76. protected function _getparamkeystring ($params) {
  77. $keys = Array_keys ($params);
  78. Return implode (",", $keys);
  79. }
  80. protected function _getparamvalstring ($params) {
  81. $vals = Array_values ($params);
  82. Return "'". Implode ("', '", $vals). "'";
  83. }
  84. Private Function _getupdatestring ($params) {
  85. echo "_getupdatestring";
  86. $sql = "";
  87. if (Is_array ($params)) {
  88. $sql = $this->_getkeyvalstring ($params, ",");
  89. }
  90. return $sql;
  91. }
  92. Private Function _getwherestring ($params) {
  93. echo "_getwherestring";
  94. $sql = "";
  95. if (Is_array ($params)) {
  96. $sql = "where";
  97. $where = $this->_getkeyvalstring ($params, "and");
  98. $sql = $sql. $where;
  99. }
  100. return $sql;
  101. }
  102. Private Function _getkeyvalstring ($params, $split) {
  103. $str = "";
  104. if (Is_array ($params)) {
  105. $PARAMARR = Array ();
  106. foreach ($params as $key = = $val) {
  107. $valstr = $val;
  108. if (is_string ($val)) {
  109. $valstr = "'". $val. "'";
  110. }
  111. $PARAMARR [] = $key. " = ". $valstr;
  112. }
  113. $str = $str. Implode ($split, $PARAMARR);
  114. }
  115. return $str;
  116. }
  117. Public Function release () {
  118. @mysql_close ();
  119. }
  120. }
  121. function T ($table) {
  122. Return (new Simpledao ())->table ($table);
  123. }
  124. ?>
Copy CodeCode snippet that uses Simpledao
  1. Include "test/simpledao.php";
  2. $dao = T ("Sw_post");
  3. $result = $dao->get ();//get all Posts
  4. $dao->release ();
  5. echo Json_encode ($result);
  6. ?>
  7. Include "test/simpledao.php";
  8. $dao = T ("Sw_post");
  9. Update title where id=1
  10. $cnt = $dao->update (Array ("title" = "Hello REST2"), Array ("id" =>1));
  11. $dao->release ();
  12. echo Json_encode (Array ("count" = $cnt));
  13. ?>
  14. Include "test/simpledao.php";
  15. $dao = T ("Sw_tag");
  16. Insert new record
  17. $cnt = $dao->insert (Array ("PostID" =>4, "name" = "Test Tag"));
  18. $dao->release ();
  19. echo Json_encode (Array ("count" = $cnt));
  20. ?>
  21. Include "test/simpledao.php";
  22. $dao = T ("Sw_tag");
  23. Delete from table where name= ' Test tag '
  24. $cnt = $dao->delete (Array ("name" = "Test Tag"));
  25. $dao->release ();
  26. echo Json_encode (Array ("count" = $cnt));
  27. ?>
Copy Code
PHP, MySQL, DAO
  • 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.