PHP database operations

Source: Internet
Author: User
Tags php database rtrim
PHP database operations

  1. /* ===================================================== ===================================== */
  2. /* File name: BaseLogic. class. php */
  3. /* Summary: common category of data processing .*/
  4. Class BaseLogic extends MyDB {
  5. Protected $ tabName; // table name
  6. Protected $ fieldList; // A set of fields
  7. Protected $ messList;
  8. // ================================================ ====
  9. // Function: add ($ postList)
  10. // Function: add
  11. // Parameter: $ postList submitted variable list
  12. // Return: the newly inserted auto-increment ID
  13. // ================================================ ====
  14. Function add ($ postList ){
  15. $ FieldList = '';
  16. $ Value = '';
  17. Foreach ($ postList as $ k => $ v ){
  18. If (in_array ($ k, $ this-> fieldList )){
  19. $ FieldList. = $ k .",";
  20. If (! Get_magic_quotes_gpc ())
  21. $ Value. = "'". addslashes ($ v )."',";
  22. Else
  23. $ Value. = "'". $ v ."',";
  24. }
  25. }
  26. $ FieldList = rtrim ($ fieldList ,",");
  27. $ Value = rtrim ($ value ,",");
  28. $ SQL = "INSERT INTO {$ this-> tabName} (". $ fieldList. ") VALUES (". $ value .")";
  29. Echo $ SQL;
  30. $ Result = $ this-> mysqli-> query ($ SQL );
  31. If ($ result & $ this-> mysqli-> affected_rows> 0)
  32. Return $ this-> mysqli-> insert_id;
  33. Else
  34. Return false;
  35. }
  36. // ================================================ ====
  37. // Function: mod ($ postList)
  38. // Function: modify table data
  39. // Parameter: $ postList submitted variable list
  40. // ================================================ ====
  41. Function mod ($ postList ){
  42. $ Id = $ postList ["id"];
  43. Unset ($ postList ["id"]);
  44. $ Value = '';
  45. Foreach ($ postList as $ k => $ v ){
  46. If (in_array ($ k, $ this-> fieldList )){
  47. If (! Get_magic_quotes_gpc ())
  48. $ Value. = $ k. "= '". addslashes ($ v )."',";
  49. Else
  50. $ Value. = $ k. "= '". $ v ."',";
  51. }
  52. }
  53. $ Value = rtrim ($ value ,",");
  54. $ SQL = "UPDATE {$ this-> tabName} SET {$ value} WHERE id = {$ id }";
  55. Return $ this-> mysqli-> query ($ SQL );
  56. }
  57. // ================================================ ====
  58. // Function: del ($ id)
  59. // Function: delete
  60. // Parameter: $ id number or ID list array
  61. // Return: 0 indicates the number of records deleted when the failure is successful.
  62. // ================================================ ====
  63. Function del ($ id ){
  64. If (is_array ($ id ))
  65. $ Tmp = "IN (". join (",", $ id ).")";
  66. Else
  67. $ Tmp = "= $ id ";
  68. $ SQL = "DELETE FROM {$ this-> tabName} WHERE id". $ tmp;
  69. Return $ this-> mysqli-> query ($ SQL );
  70. }
  71. Function get ($ id ){
  72. $ SQL = "SELECT * FROM {$ this-> tabName} WHERE id = {$ id }";
  73. $ Result = $ this-> mysqli-> query ($ SQL );
  74. If ($ result & $ result-> num_rows = 1 ){
  75. Return $ result-> fetch_assoc ();
  76. } Else {
  77. Return false;
  78. }
  79. }
  80. Function getMessList (){
  81. $ Message = "";
  82. If (! Empty ($ this-> messList )){
  83. Foreach ($ this-> messList as $ value ){
  84. $ Message. = $ value ."
    ";
  85. }
  86. }
  87. Return $ message;
  88. }
  89. }
  90. ?>


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.