很好用的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对图像的各种处理函数代码小结
Jul 08 PHP
PHP自动识别字符集并完成转码详解
Aug 02 PHP
php实现利用phpexcel导出数据
Aug 24 PHP
php sybase_fetch_array使用方法
Apr 15 PHP
PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法
Dec 17 PHP
php魔术函数__call()用法实例分析
Feb 13 PHP
php从数据库查询结果生成树形列表的方法
Apr 17 PHP
php使用GD实现颜色渐变实例
Jun 02 PHP
php读取qqwry.dat ip地址定位文件的类实例代码
Nov 15 PHP
php微信开发之关键词回复功能
Jun 13 PHP
使用PHPWord生成word文档的方法详解
Jun 06 PHP
php 输出缓冲 Output Control用法实例详解
Mar 03 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中函数rand和mt_rand的区别比较
2012/12/26 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
PHP中TP5 上传文件的实例详解
2017/07/31 PHP
33个优秀的 jQuery 图片展示插件分享
2012/03/14 Javascript
poshytip 基于jquery的 插件 主要用于显示微博人的图像和鼠标提示等
2012/10/12 Javascript
jquery等宽输出文字插件使用介绍
2013/09/18 Javascript
jQuery实现强制cookie过期方法汇总
2015/05/22 Javascript
require.js的用法详解
2015/10/20 Javascript
深入剖析JavaScript面向对象编程
2016/07/12 Javascript
详解JS中的快速排序与冒泡
2017/01/10 Javascript
self.attachevent is not a function的解决方法
2017/04/04 Javascript
Vue 2中ref属性的使用方法及注意事项
2017/06/12 Javascript
JavaScript利用fetch实现异步请求的方法实例
2017/07/26 Javascript
vue中v-model动态生成的实例详解
2017/10/27 Javascript
vue中的自定义分页插件组件的示例
2018/08/18 Javascript
taro小程序添加骨架屏的实现代码
2019/11/15 Javascript
原生JavaScript实现的无缝滚动功能详解
2020/01/17 Javascript
通过vue.extend实现消息提示弹框的方法记录
2021/01/07 Vue.js
[11:01]2014DOTA2西雅图邀请赛 冷冷带你探秘威斯汀
2014/07/08 DOTA
[00:21]DOTA2亚洲邀请赛 Logo演绎
2015/02/07 DOTA
python 随机数使用方法,推导以及字符串,双色球小程序实例
2017/09/12 Python
Python编程之Re模块下的函数介绍
2017/10/28 Python
Python中pillow知识点学习
2018/04/30 Python
django连接oracle时setting 配置方法
2019/08/29 Python
Django 如何使用日期时间选择器规范用户的时间输入示例代码详解
2020/05/22 Python
H5 canvas中width、height和style的宽高区别详解
2018/11/02 HTML / CSS
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
精选鞋类、服装和配饰的全球领先目的地:Bodega
2021/02/27 全球购物
2013届毕业生求职信范文
2013/11/20 职场文书
中专三年学习的个人自我评价
2013/12/12 职场文书
幼儿园母亲节活动方案
2014/03/10 职场文书
法人授权委托书格式
2014/04/08 职场文书
2014年工作总结及2015工作计划
2014/12/12 职场文书
用电申请报告范文
2015/05/18 职场文书
学籍证明模板
2015/06/18 职场文书
CSS SandBox应用场景及常见问题
2022/06/25 HTML / CSS