Discuz教程网

很好用的PHP数据库类

[复制链接]
authicon dly 发表于 2011-9-5 20:21:33 | 显示全部楼层 |阅读模式
代码如下:

  1. <?
  2. //很好用的PHP数据库类,三、四句代码搞定一个表的操作,无论这个表字段有多复杂。
  3. //此类多次大量用在大型网站程序的开发上,效果特别的好。
  4. //作者:快刀浪子++ 
  5. define("_PHP_RECORD_","exists");
  6. class TRecord
  7. {
  8. var $db;
  9. var $rc;
  10. var $name;
  11. var $value;
  12. var $num;
  13. var $buffer;   //查询结果 调用方法 $buffer[$i]["fields"];
  14. var $seekstr;   //保存查询条件用
  15. function TRecord($host="localhost",$user="root",$passwd="")
  16. {global $HTTP_POST_VARS;
  17. $this->num=0;
  18. $this->host=$host;
  19. $this->user=$user;
  20. $this->passwd=$passwd;
  21. if(($this->db=mysql_connect($host,$user,$passwd))==false)
  22. exit("联结数据库出错!");
  23.   while(list($this->name[$this->num],$this->value[$this->num])=each($HTTP_POST_VARS))
  24. {$this->num++;
  25. }
  26. //////////////
  27. for($i=0;$i<$this->num;$i++)
  28. {$this->value[$i]=$this->SafeString($this->value[$i]);
  29. }
  30. //
  31. }
  32. function SafeString($message)
  33. {$message=str_replace(" "," ",$message);
  34. $message=str_replace("<","<",$message);
  35. $message=str_replace(">",">",$message);
  36. //$message=str_replace("|","|",$message);
  37. //$message=str_replace("\"",""",$message);
  38. //$message=nl2br($message);
  39. return $message;
  40. }
  41. //////
  42. function reset()
  43. {$this->num=0;
  44. $this->name=array();
  45.    $this->value=array();
  46. }
  47. function add($name,$values)
  48. {$this->name[$this->num]=$name;
  49.    $this->value[$this->num]=$values;
  50. $this->num++;
  51. }
  52. function unadd($name)
  53. {$j=0;
  54. for($i=0;$i<$this->num;$i++)
  55. {if($this->name[$i]!=$name)
  56. {$aaa[$j]=$this->name[$i];
  57. $bbb[$j]=$this->value[$i];
  58. $j++;
  59. }
  60. }
  61. $this->name=$aaa;
  62. $this->value=$bbb;
  63. $this->num=$j;
  64. }
  65. function InsertRecord($database,$table)
  66. {mysql_select_db($database);
  67. if($this->num==0)
  68. exit("没有定义变量!");
  69. $field=implode(",",$this->name);
  70. for($i=0;$i<$this->num;$i++)
  71. {if(is_string($this->value[$i]))
  72. $ls[$i]="\'".$this->value[$i]."\'";
  73.  else
  74. $ls[$i]=$this->value[$i];
  75.      $value=implode(",",$ls);  
  76. }
  77. $sql=sprintf("insert into %s(%s) values(%s)",$table,$field,$value);
  78. if(mysql_query($sql,$this->db)==false)
  79. {echo "写数据到数据库时出错:".$sql;
  80. exit();
  81. }
  82. }
  83. function SelectRecord($database,$table) //返回记录数,结果在缓冲区中
  84. {mysql_select_db($database);
  85.     if($this->num==0)
  86. $sql=sprintf("select * from %s",$table);
  87.  else
  88. {
  89. for($i=0;$i<$this->num;$i++)
  90. {if(is_string($this->value[$i]))
  91. $ls[$i]="\'".$this->value[$i]."\'";
  92.    else
  93. $ls[$i]=$this->value[$i];
  94. $str[$i]=sprintf("%s=%s",$this->name[$i],$ls[$i]);
  95. }
  96. $string=implode(" and ",$str);
  97. $this->seekstr=$string;
  98. $sql=sprintf("select * from %s where %s",$table,$string);
  99. }
  100. if(($rc=mysql_query($sql,$this->db))==false)
  101. {echo "查询数据库时出错:".$sql;
  102. exit();
  103. }
  104. $i=0;
  105. while($this->buffer[$i]=mysql_fetch_array($rc))
  106. {
  107. $i++;
  108. }
  109. mysql_free_result($rc);
  110. return $i;
  111. }
  112. function UpdateRecord($database,$table,$limitstr)
  113. {mysql_select_db($database);
  114. if($this->num==0)
  115. exit("没有定义变量!");
  116. for($i=0;$i<$this->num;$i++)
  117. {if(is_string($this->value[$i]))
  118. $ls[$i]="\'".$this->value[$i]."\'";
  119.  else
  120. $ls[$i]=$this->value[$i];
  121. $upstr[$i]=$this->name[$i]."=".$ls[$i];
  122. }
  123.     $str=implode(",",$upstr);
  124. $sql=sprintf("update %s set %s where %s",$table,$str,$limitstr);
  125. if(mysql_query($sql,$this->db)==false)
  126. {echo "修改数据时出错:".$sql;
  127. exit();
  128. }
  129. }
  130. function addtip($database,$table,$fileds,$limitstr="")
  131. {//必须为整型字段 
  132. mysql_select_db($database);
  133. if($limitstr!="")
  134. $sql=sprintf("update %s set %s=%s+1 where %s",$table,$fileds,$fileds,$limitstr);
  135.  else
  136. $sql=sprintf("update %s set %s=%s+1",$table,$fileds,$fileds);
  137. if(mysql_query($sql,$this->db)==false)
  138. {echo "修改数据时出错:".$sql;
  139. exit();
  140. }
  141. }
  142. function unaddtip($database,$table,$fileds,$limitstr="")
  143. {
  144. mysql_select_db($database);
  145. if($limitstr!="")
  146. $sql=sprintf("update %s set %s=%s-1 where %s",$table,$fileds,$fileds,$limitstr);
  147.  else
  148. $sql=sprintf("update %s set %s=%s-1",$table,$fileds,$fileds);
  149. if(mysql_query($sql,$this->db)==false)
  150. {echo "修改数据时出错:".$sql;
  151. exit();
  152. }
  153. }
  154. function isempty($var,$china)
  155. {if(trim($var)=="")
  156. {
  157. $reason="没有录入“".$china."”!";
  158. exit($reason);
  159. }
  160. }
  161. function GetResult()
  162. {return $this->buffer;
  163. }
  164. function close()
  165. {
  166. mysql_close($this->db);
  167. }
  168. }
  169. ?>
复制代码








上一篇:一个很不错的PHP翻页类
下一篇:PHP XML备份Mysql数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 06:58

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表