Simple dao class based on php and mysql

Source: Internet
Author: User
Simple dao class based on php and mysql
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. ?>

Code snippet using 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. ?>

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.