欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个php的mysql操作类

程序员文章站 2022-04-26 19:57:20
...
  1. //数据库操作类

  2. class db
  3. {
  4. //SQL执行后的数据保存变量;
  5. var $db;
  6. //读取或设置当前数据的位置
  7. var $position=0;
  8. //执行SQL语句并把结果保存为db变量中;
  9. function sub_sql($str)

  10. {
  11. global $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. //读取一条数据并把数据往后移一位,如果数据为空则返回为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. //判断是否数据读取到结尾了
  38. function Judge()
  39. {
  40. $re=empty($this->db[$this->position])?true:false;
  41. return $re;
  42. }
  43. //取得db里面的个数
  44. function Get_Num()
  45. {
  46. return count($this->db);
  47. }
  48. //更新数据库里面的数据,$t为表名,$v格式为数组格式,上标为字段名,下标为数据;$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
  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=" break;
  78. }
  79. if(!empty($f))
  80. {
  81. foreach($w as $key=>$vaule)
  82. {
  83. if(!is_numeric($key))
  84. {
  85. if(empty($v_str))
  86. {
  87. $w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
  88. }else
  89. {
  90. $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
  91. }
  92. }
  93. }
  94. }
  95. $sql="UPDATE ".$t." SET ".$v_str." where ".$w_str;
  96. return $result = mysql_query($sql);
  97. }
  98. //删除一数据$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
  99. function Set_Del($t,$w,$p=0)
  100. {
  101. $this->sub_sql($t);
  102. $w_str="";
  103. $f="";
  104. switch($p)
  105. {
  106. case 0:
  107. $f="=";
  108. break;
  109. case 1:
  110. $f=">";
  111. break;
  112. case -1:
  113. $f=" break;
  114. }
  115. if(!empty($f))
  116. {
  117. foreach($w as $key=>$vaule)
  118. {
  119. if(!is_numeric($key))
  120. {
  121. if(empty($v_str))
  122. {
  123. $w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
  124. }else
  125. {
  126. $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
  127. }
  128. }
  129. }
  130. }
  131. $str="DELETE FROM ".$t." WHERE ".$w_str;
  132. return $result = mysql_query($str);
  133. }
  134. function Add($t,$v)
  135. {
  136. $this->sub_sql($t);
  137. $k_str="";
  138. $v_str="";
  139. foreach($v as $key=>$vaule)
  140. {
  141. if(!is_numeric($key)){
  142. if(empty($k_str))
  143. {
  144. $k_str=htmlspecialchars($key);
  145. $v_str="'".htmlspecialchars($vaule)."'";
  146. }else
  147. {
  148. $k_str=$k_str.",".htmlspecialchars($key);
  149. $v_str=$v_str.","."'".htmlspecialchars($vaule)."'";
  150. }
  151. }
  152. }
  153. $str="INSERT INTO ".$t."(".$k_str.")"."value(".$v_str.")";
  154. return $result = mysql_query($str);
  155. }
  156. }
  157. ?>
复制代码