很好用的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安全编程之加密功能
Oct 09 PHP
php 禁止页面缓存输出
Jan 07 PHP
php递归实现无限分类生成下拉列表的函数
Aug 08 PHP
PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法
Jun 26 PHP
php实现图片转换成ASCII码的方法
Apr 03 PHP
Yii实现Command任务处理的方法详解
Jul 14 PHP
PHP Callable强制指定回调类型的方法
Aug 30 PHP
PHP运行模式汇总
Nov 06 PHP
zend框架实现支持sql server的操作方法
Dec 08 PHP
CI(CodeIgniter)框架视图中加载视图的方法
Mar 24 PHP
CMSPRESS 10行代码搞定 PHP无限级分类2
Mar 30 PHP
PHP检查文件是否存在,不存在自动创建及读取文件内容操作示例
Jan 23 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
生成缩略图
2006/10/09 PHP
PHP 导出数据到淘宝助手CSV的方法分享
2010/02/27 PHP
ThinkPHP跳转页success及error模板实例教程
2014/07/17 PHP
在Linux系统下一键重新安装WordPress的脚本示例
2015/06/30 PHP
php开发时容易忘记的一些技术细节
2016/02/03 PHP
Laravel框架模板加载,分配变量及简单路由功能示例
2018/06/11 PHP
JS 无法通过W3C验证的处理方法
2010/03/09 Javascript
Jquery刷新页面背景图片随机变换的实现方法
2013/03/15 Javascript
JS实现跟随鼠标的链接文字提示框效果
2015/08/06 Javascript
前端js弹出框组件使用方法
2020/08/24 Javascript
你可能不知道的JSON.stringify()详解
2017/08/17 Javascript
weebox弹出窗口不居中显示的解决方法
2017/11/27 Javascript
js实现轮播图效果 z-index实现轮播图
2020/01/17 Javascript
Angular利用HTTP POST下载流文件的步骤记录
2020/07/26 Javascript
[03:51]吞吞映像 每周精彩击杀top10第二弹
2014/06/25 DOTA
Python 学习笔记
2008/12/27 Python
python使用str &amp; repr转换字符串
2016/10/13 Python
Python的标准模块包json详解
2017/03/13 Python
python 类详解及简单实例
2017/03/24 Python
PyQt5每天必学之进度条效果
2018/04/19 Python
Python读取txt内容写入xls格式excel中的方法
2018/10/11 Python
Python OOP类中的几种函数或方法总结
2019/02/22 Python
Python:Numpy 求平均向量的实例
2019/06/29 Python
django2.2安装错误最全的解决方案(小结)
2019/09/24 Python
解决tensorflow读取本地MNITS_data失败的原因
2020/06/22 Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
2020/12/07 Python
香港网上花店:FlowerAdvisor香港
2019/05/30 全球购物
Bloomingdale’s阿联酋:选购奢华时尚、美容及更多
2020/09/22 全球购物
如何拷贝一整个Java对象,包括它的状态
2013/12/27 面试题
初中三年毕业生的自我评价分享
2014/02/14 职场文书
优秀大学生求职自荐信范文
2014/04/19 职场文书
取保候审保证书
2014/04/30 职场文书
在职员工证明书
2014/09/19 职场文书
民主生活会汇报材料
2014/12/15 职场文书
繁星春水读书笔记
2015/06/30 职场文书
门面租赁合同范文
2019/08/06 职场文书