很好用的PHP数据库类


Posted in PHP onMay 27, 2009
<? 
//很好用的PHP数据库类,三、四句代码搞定一个表的操作,无论这个表字段有多复杂。 
//此类多次大量用在大型网站程序的开发上,效果特别的好。 
//作者:快刀浪子++  
define(\"_PHP_RECORD_\",\"exists\"); 
class TRecord 
{ 
var $db; 
var $rc; 
var $name; 
var $value; 
var $num; 
var $buffer; //查询结果 调用方法 $buffer[$i][\"fields\"]; 
var $seekstr;
 //保存查询条件用 
function TRecord($host=\"localhost\",$user=\"root\",$passwd=\"\") 
{global $HTTP_POST_VARS; 
$this->num=0; 
$this->host=$host; 
$this->user=$user; 
$this->passwd=$passwd; 
if(($this->db=mysql_connect($host,$user,$passwd))==false) 
exit(\"联结数据库出错!\"); 

while(list($this->name[$this->num],$this->value[$this->num])=each($HTTP_POST_VARS)) 
{$this->num++; 
} 
////////////// 
for($i=0;$i<$this->num;$i++) 
{$this->value[$i]=$this->SafeString($this->value[$i]); 
} 
// 
} 
function SafeString($message) 
{$message=str_replace(\" \",\" \",$message); 
$message=str_replace(\"<\",\"<\",$message); 
$message=str_replace(\">\",\">\",$message); 
//$message=str_replace(\"|\",\"|\",$message); 
//$message=str_replace(\"\\"\",\""\",$message); 
//$message=nl2br($message); 
return $message; 
} 
////// 
function reset() 
{$this->num=0; 
$this->name=array(); 

 $this->value=array(); 
} 
function add($name,$values) 
{$this->name[$this->num]=$name; 

 $this->value[$this->num]=$values; 
$this->num++; 
} 
function unadd($name) 
{$j=0; 
for($i=0;$i<$this->num;$i++) 
{if($this->name[$i]!=$name) 
{$aaa[$j]=$this->name[$i]; 
$bbb[$j]=$this->value[$i]; 
$j++; 
} 
} 
$this->name=$aaa; 
$this->value=$bbb; 
$this->num=$j; 
} 
function InsertRecord($database,$table) 
{mysql_select_db($database); 
if($this->num==0) 
exit(\"没有定义变量!\"); 
$field=implode(\",\",$this->name); 
for($i=0;$i<$this->num;$i++) 
{if(is_string($this->value[$i])) 
$ls[$i]=\"\'\".$this->value[$i].\"\'\"; 
 else 
$ls[$i]=$this->value[$i]; 


 $value=implode(\",\",$ls);   
} 
$sql=sprintf(\"insert into %s(%s) values(%s)\",$table,$field,$value); 
if(mysql_query($sql,$this->db)==false) 
{echo \"写数据到数据库时出错:\".$sql; 
exit(); 
} 
} 
function SelectRecord($database,$table) //返回记录数,结果在缓冲区中 
{mysql_select_db($database); 


if($this->num==0) 
$sql=sprintf(\"select * from %s\",$table); 
 else 
{ 
for($i=0;$i<$this->num;$i++) 
{if(is_string($this->value[$i])) 
$ls[$i]=\"\'\".$this->value[$i].\"\'\"; 

 else 
$ls[$i]=$this->value[$i]; 
$str[$i]=sprintf(\"%s=%s\",$this->name[$i],$ls[$i]); 
} 
$string=implode(\" and \",$str); 
$this->seekstr=$string; 
$sql=sprintf(\"select * from %s where %s\",$table,$string); 
} 
if(($rc=mysql_query($sql,$this->db))==false) 
{echo \"查询数据库时出错:\".$sql; 
exit(); 
} 
$i=0; 
while($this->buffer[$i]=mysql_fetch_array($rc)) 
{ 
$i++; 
} 
mysql_free_result($rc); 
return $i; 
} 
function UpdateRecord($database,$table,$limitstr) 
{mysql_select_db($database); 
if($this->num==0) 
exit(\"没有定义变量!\"); 
for($i=0;$i<$this->num;$i++) 
{if(is_string($this->value[$i])) 
$ls[$i]=\"\'\".$this->value[$i].\"\'\"; 
 else 
$ls[$i]=$this->value[$i]; 
$upstr[$i]=$this->name[$i].\"=\".$ls[$i]; 
} 


$str=implode(\",\",$upstr); 
$sql=sprintf(\"update %s set %s where %s\",$table,$str,$limitstr); 
if(mysql_query($sql,$this->db)==false) 
{echo \"修改数据时出错:\".$sql; 
exit(); 
} 
} 
function addtip($database,$table,$fileds,$limitstr=\"\") 
{//必须为整型字段  
mysql_select_db($database); 
if($limitstr!=\"\") 
$sql=sprintf(\"update %s set %s=%s+1 where %s\",$table,$fileds,$fileds,$limitstr); 
 else 
$sql=sprintf(\"update %s set %s=%s+1\",$table,$fileds,$fileds); 
if(mysql_query($sql,$this->db)==false) 
{echo \"修改数据时出错:\".$sql; 
exit(); 
} 
} 
function unaddtip($database,$table,$fileds,$limitstr=\"\") 
{ 
mysql_select_db($database); 
if($limitstr!=\"\") 
$sql=sprintf(\"update %s set %s=%s-1 where %s\",$table,$fileds,$fileds,$limitstr); 
 else 
$sql=sprintf(\"update %s set %s=%s-1\",$table,$fileds,$fileds); 
if(mysql_query($sql,$this->db)==false) 
{echo \"修改数据时出错:\".$sql; 
exit(); 
} 
} 
function isempty($var,$china) 
{if(trim($var)==\"\") 
{ 
$reason=\"没有录入“\".$china.\"”!\"; 
exit($reason); 
} 
} 
function GetResult() 
{return $this->buffer; 
} 
function close() 
{ 
mysql_close($this->db); 
} 
} 
?>
PHP 相关文章推荐
php下使用以下代码连接并测试
Apr 09 PHP
php session 错误
May 21 PHP
phpMyAdmin 链接表的附加功能尚未激活的问题
Aug 01 PHP
PHP 文件系统详解
Sep 13 PHP
2014过年倒计时示例
Jan 31 PHP
php的webservice的wsdl的XML无法显示问题的解决方法
Mar 11 PHP
在PHP中使用X-SendFile头让文件下载更快
Jun 01 PHP
destoon后台网站设置变成空白的解决方法
Jun 21 PHP
php仿微信红包分配算法的实现方法
May 13 PHP
PHP设计模式之单例模式原理与实现方法分析
Apr 25 PHP
详解PHP中curl_multi并发的实现
Jun 08 PHP
Laravel服务容器绑定的几种方法总结
Jun 14 PHP
PHP XML备份Mysql数据库
May 27 #PHP
PHP mail 通过Windows的SMTP发送邮件失败的解决方案
May 27 #PHP
php 字符转义 注意事项
May 27 #PHP
php 字符过滤类,用于过滤各类用户输入的数据
May 27 #PHP
PHP的单引号和双引号 字符串效率
May 27 #PHP
php session 错误
May 21 #PHP
php print EOF实现方法
May 21 #PHP
You might like
php 格式化数字的时候注意数字的范围
2010/04/13 PHP
比较discuz和ecshop的截取字符串函数php版
2012/09/03 PHP
PHP实现把数字ID转字母ID
2013/08/12 PHP
php获取twitter最新消息的方法
2015/04/14 PHP
Zend Framework入门应用实例详解
2016/12/11 PHP
PHP串行化与反串行化实例分析
2016/12/27 PHP
ThinkPHP Where 条件中常用表达式示例(详解)
2017/03/31 PHP
简单实现php上传文件功能
2017/09/21 PHP
Laravel+Intervention实现上传图片功能示例
2019/07/09 PHP
JS网络游戏-(模拟城市webgame)提供的一些例子下载
2007/10/14 Javascript
jQuery Ajax 全解析
2009/02/08 Javascript
js函数中onmousedown和onclick的区别和联系探讨
2013/05/19 Javascript
js 左右悬浮对联广告特效代码
2014/12/12 Javascript
javascript函数特点实例分析
2015/05/14 Javascript
node.js cookie-parser 中间件介绍
2016/06/06 Javascript
EasyUI Pagination 分页的两种做法小结
2016/07/09 Javascript
Vue.js实现微信过渡动画左右切换效果
2017/06/13 Javascript
js中变量的连续赋值(实例讲解)
2017/07/08 Javascript
Angularjs实现下拉框联动的示例代码
2017/08/22 Javascript
vue+iview框架实现左侧动态菜单功能的示例代码
2020/07/23 Javascript
深入理解Python中的内置常量
2017/05/20 Python
zookeeper python接口实例详解
2018/01/18 Python
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
2018/02/03 Python
pandas 如何分割字符的实现方法
2019/07/29 Python
django中使用事务及接入支付宝支付功能
2019/09/15 Python
推荐8款常用的Python GUI图形界面开发框架
2020/02/23 Python
python程序文件扩展名知识点详解
2020/02/27 Python
python os模块常用的29种方法使用详解
2020/06/02 Python
如何用python批量调整视频声音
2020/12/22 Python
通信工程专业个人找工作求职信范文
2013/09/21 职场文书
成功的酒店创业计划书
2013/12/27 职场文书
公务员更新知识培训实施方案
2014/03/31 职场文书
公证委托书格式
2014/09/13 职场文书
浅谈如何写好演讲稿?
2019/06/12 职场文书
Python爬虫之爬取最新更新的小说网站
2021/05/06 Python
Pytorch数据读取之Dataset和DataLoader知识总结
2021/05/23 Python