A php mysql operation class

Source: Internet
Author: User
Tags php mysql
A php mysql operation class

  1. // Database operations

  2. Class db
  3. {
  4. // Save the data variable after SQL execution;
  5. Var $ db;
  6. // Read or set the current data location
  7. Var $ position = 0;
  8. // Execute the SQL statement and save the result as a db variable;

  9. Function sub_ SQL ($ str)

  10. {
  11. Global $ prefix; // global function, table prefix
  12. Return str_replace ("#@__", $ prefix, $ str );
  13. }
  14. Function SQL ($ str)
  15. {
  16. $ Str = $ this-> sub_ SQL ($ str );
  17. $ Result = mysql_query ($ str );
  18. $ I = 0;
  19. While ($ row = mysql_fetch_array ($ result ))
  20. {
  21. $ Str_array [$ I] = $ row;
  22. $ I ++;
  23. }
  24. If (empty ($ str_array ))
  25. {
  26. $ Str_array = array ();
  27. }
  28. $ This-> db = $ str_array;
  29. }
  30. // Read a piece of data and move the data back one bit. if the data is empty, null is returned;
  31. Function Get_One ()
  32. {
  33. $ Re = empty ($ this-> db [$ this-> position])? Null: $ this-> db [$ this-> position];
  34. $ This-> position = $ re? $ This-> position + 1: $ this-> position;
  35. Return $ re;
  36. }
  37. // Determine whether the data has been read to the end
  38. Function Judge ()
  39. {
  40. $ Re = empty ($ this-> db [$ this-> position])? True: false;
  41. Return $ re;
  42. }
  43. // Obtain the number of databases
  44. Function Get_Num ()
  45. {
  46. Return count ($ this-> db );
  47. }
  48. // Update the data in the database. $ t indicates the table name, $ v indicates the array format, with the field name and subscript representing the data. $ w indicates that the condition is labeled as the field name subscript as the data, $ p indicates that the condition 0 is equal, 1 is greater than, and-1 is less;
  49. Function Set_Updata ($ t, $ v, $ w, $ p = 0)
  50. {
  51. $ This-> SQL ($ t );
  52. $ V_str = "";
  53. $ W_str = "";
  54. $ F = "";
  55. Foreach ($ v as $ key => $ vaule)
  56. {
  57. If (! Is_numeric ($ key ))
  58. {
  59. If (empty ($ v_str ))
  60. {
  61. $ V_str = htmlspecialchars ($ key). "= '". htmlspecialchars ($ vaule )."'";
  62. } Else
  63. {
  64. $ V_str = $ v_str. ",". htmlspecialchars ($ key). "= '". htmlspecialchars ($ vaule )."'";
  65. }
  66. }
  67. }
  68. Switch ($ p)
  69. {
  70. Case 0:
  71. $ F = "= ";
  72. Break;
  73. Case 1:
  74. $ F = "> ";
  75. Break;
  76. Case-1:
  77. $ F = "<";
  78. Break;
  79. }
  80. If (! Empty ($ f ))
  81. {
  82. Foreach ($ w as $ key => $ vaule)
  83. {
  84. If (! Is_numeric ($ key ))
  85. {
  86. If (empty ($ v_str ))
  87. {
  88. Invalid w_str=htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
  89. } Else
  90. {
  91. $ W_str = $ w_str. "," .htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
  92. }
  93. }
  94. }
  95. }
  96. $ SQL = "UPDATE". $ t. "SET". $ v_str. "where". $ w_str;
  97. Return $ result = mysql_query ($ SQL );
  98. }
  99. // Delete a Data $ w indicates that the field name subscript is the data, $ p indicates that the condition 0 is equal, 1 is greater than, and-1 is less;
  100. Function Set_Del ($ t, $ w, $ p = 0)
  101. {
  102. $ This-> sub_ SQL ($ t );
  103. $ W_str = "";
  104. $ F = "";
  105. Switch ($ p)
  106. {
  107. Case 0:
  108. $ F = "= ";
  109. Break;
  110. Case 1:
  111. $ F = "> ";
  112. Break;
  113. Case-1:
  114. $ F = "<";
  115. Break;
  116. }
  117. If (! Empty ($ f ))
  118. {
  119. Foreach ($ w as $ key => $ vaule)
  120. {
  121. If (! Is_numeric ($ key ))
  122. {
  123. If (empty ($ v_str ))
  124. {
  125. Invalid w_str=htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
  126. } Else
  127. {
  128. $ W_str = $ w_str. "," .htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
  129. }
  130. }
  131. }
  132. }
  133. $ Str = "delete from". $ t. "WHERE". $ w_str;
  134. Return $ result = mysql_query ($ str );
  135. }
  136. Function Add ($ t, $ v)
  137. {
  138. $ This-> sub_ SQL ($ t );
  139. $ K_str = "";
  140. $ V_str = "";
  141. Foreach ($ v as $ key => $ vaule)
  142. {
  143. If (! Is_numeric ($ key )){
  144. If (empty ($ k_str ))
  145. {
  146. $ K_str = htmlspecialchars ($ key );
  147. $ V_str = "'". htmlspecialchars ($ vaule )."'";
  148. } Else
  149. {
  150. $ K_str = $ k_str. ",". htmlspecialchars ($ key );
  151. $ V_str = $ v_str. ",". "'". htmlspecialchars ($ vaule )."'";
  152. }
  153. }
  154. }
  155. $ Str = "insert into". $ t. "(". $ k_str. ")". "value (". $ v_str .")";
  156. Return $ result = mysql_query ($ str );
  157. }
  158. }
  159. ?>

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.