Home > Backend Development > PHP Tutorial > A PHP mysql operation class

A PHP mysql operation class

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:03:08
Original
1087 people have browsed it
  1. //Database operation class

  2. class db
  3. {
  4. //Data saving variables after SQL execution;
  5. var $db;
  6. //Read or set current data Position
  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, and return if the data is empty is null;
  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. //Judge 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. //Get the number in db
  44. function Get_Num()
  45. {
  46. return count($this->db);
  47. }
  48. //Update the data in the database, $t is the table name, $v format is the array format, the superscript is the field name, and the subscript is the data; $w is The superscript of the condition is the field name and the subscript is the data. $p is the condition. 0 is the equal sign, 1 is greater than, -1 is less than;
  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. $ w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
  89. }else
  90. {
  91. $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($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 piece of data. $w is the condition. The superscript is the field name and the subscript is the data. $p is the condition. 0 is the equal sign, 1 is greater than, -1 is less than;
  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. $w_str=htmlspecialchars($key ).$f.htmlspecialchars($vaule)."'";
  126. }else
  127. {
  128. $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($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. ?>

复制代码


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template