很好用的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面向对象全攻略 (十一)__toString()用法 克隆对象 __call处理调用错误
Sep 30 PHP
php visitFile()遍历指定文件夹函数
Aug 21 PHP
通过JavaScript或PHP检测Android设备的代码
Mar 09 PHP
PHP curl 并发最佳实践代码分享
Sep 05 PHP
如何用php获取程序执行的时间
Jun 09 PHP
PHP echo,print,printf,sprintf函数之间的区别与用法详解
Nov 27 PHP
Codeigniter的一些优秀特性总结
Jan 21 PHP
php创建、获取cookie及基础要点分析
Jan 26 PHP
php实现有趣的人品测试程序实例
Jun 08 PHP
CodeIgniter多语言实现方法详解
Jan 20 PHP
CodeIgniter表单验证方法实例详解
Mar 03 PHP
php curl操作API接口类完整示例
May 21 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 array_walk() 数组函数
2011/07/12 PHP
thinkphp3.2中实现phpexcel导出带生成图片示例
2017/02/14 PHP
php实现对短信验证码发送次数的限制实例讲解
2021/03/04 PHP
用prototype实现的简单小巧的多级联动菜单
2007/03/24 Javascript
让IE6支持min-width和max-width的方法
2010/06/25 Javascript
Jquery的hover方法让鼠标经过li时背景变色
2013/09/06 Javascript
JavaScript 判断用户输入的邮箱及手机格式是否正确
2013/12/08 Javascript
IE6浏览器中window.location.href无效的解决方法
2014/11/20 Javascript
JS函数修改html的元素内容,及修改属性内容的方法
2016/10/28 Javascript
浅谈Vue的加载顺序探讨
2017/10/25 Javascript
浅谈vue中改elementUI默认样式引发的static与assets的区别
2018/02/03 Javascript
JavaScript中call和apply方法的区别实例分析
2018/08/03 Javascript
vue中各选项及钩子函数执行顺序详解
2018/08/25 Javascript
layer ui 导入文件之前传入数据的实例
2019/09/23 Javascript
jQuery实现的移动端图片缩放功能组件示例
2020/05/01 jQuery
JavaScript cookie原理及使用实例
2020/05/08 Javascript
[01:59]深扒TI7聊天轮盘语音出处 1
2017/05/11 DOTA
[50:54]完美世界DOTA2联赛 GXR vs IO 第三场 11.07
2020/11/10 DOTA
python使用paramiko实现远程拷贝文件的方法
2016/04/18 Python
举例讲解Python编程中对线程锁的使用
2016/07/12 Python
python的dataframe转换为多维矩阵的方法
2018/04/11 Python
python3.5 email实现发送邮件功能
2018/05/22 Python
OpenCV3.0+Python3.6实现特定颜色的物体追踪
2019/07/23 Python
Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并
2019/08/28 Python
Python自动化测试中yaml文件读取操作
2020/08/20 Python
python pymysql库的常用操作
2020/10/16 Python
如何利用python正则表达式匹配版本信息
2020/12/09 Python
西班牙最大的婴儿用品网上商店:Bebitus
2019/05/30 全球购物
小学教师的个人自我鉴定
2013/10/26 职场文书
医院辞职信范文
2014/01/17 职场文书
2014年元旦促销活动方案
2014/02/22 职场文书
校本教研活动总结
2014/07/01 职场文书
社保缴纳证明申请书
2014/11/03 职场文书
毕业生就业推荐表导师评语
2014/12/31 职场文书
二十年同学聚会感言
2015/07/30 职场文书
教你利用python实现企业微信发送消息
2021/05/23 Python