很好用的PHP數(shù)據(jù)庫(kù)類
更新時(shí)間:2009年05月27日 17:34:30 作者:
很好用的PHP數(shù)據(jù)庫(kù)類,三、四句代碼搞定一個(gè)表的操作,無(wú)論這個(gè)表字段有多復(fù)雜。
復(fù)制代碼 代碼如下:
<?
//很好用的PHP數(shù)據(jù)庫(kù)類,三、四句代碼搞定一個(gè)表的操作,無(wú)論這個(gè)表字段有多復(fù)雜。
//此類多次大量用在大型網(wǎng)站程序的開發(fā)上,效果特別的好。
//作者:快刀浪子++
define(\"_PHP_RECORD_\",\"exists\");
class TRecord
{
var $db;
var $rc;
var $name;
var $value;
var $num;
var $buffer; //查詢結(jié)果 調(diào)用方法 $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(\"聯(lián)結(jié)數(shù)據(jù)庫(kù)出錯(cuò)!\");
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(\"沒(méi)有定義變量!\");
$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 \"寫數(shù)據(jù)到數(shù)據(jù)庫(kù)時(shí)出錯(cuò):\".$sql;
exit();
}
}
function SelectRecord($database,$table) //返回記錄數(shù),結(jié)果在緩沖區(qū)中
{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 \"查詢數(shù)據(jù)庫(kù)時(shí)出錯(cuò):\".$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(\"沒(méi)有定義變量!\");
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 \"修改數(shù)據(jù)時(shí)出錯(cuò):\".$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 \"修改數(shù)據(jù)時(shí)出錯(cuò):\".$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 \"修改數(shù)據(jù)時(shí)出錯(cuò):\".$sql;
exit();
}
}
function isempty($var,$china)
{if(trim($var)==\"\")
{
$reason=\"沒(méi)有錄入“\".$china.\"”!\";
exit($reason);
}
}
function GetResult()
{return $this->buffer;
}
function close()
{
mysql_close($this->db);
}
}
?>
您可能感興趣的文章:
- php實(shí)現(xiàn)的簡(jiǎn)單數(shù)據(jù)庫(kù)操作Model類
- PHP簡(jiǎn)單數(shù)據(jù)庫(kù)操作類實(shí)例【支持增刪改查及鏈?zhǔn)讲僮鳌?/a>
- 全新的PDO數(shù)據(jù)庫(kù)操作類php版(僅適用Mysql)
- PHP+Mysql樹型結(jié)構(gòu)(無(wú)限分類)數(shù)據(jù)庫(kù)設(shè)計(jì)的2種方式實(shí)例
- PHP數(shù)據(jù)庫(kù)操作之基于Mysqli的數(shù)據(jù)庫(kù)操作類庫(kù)
- php db類庫(kù)進(jìn)行數(shù)據(jù)庫(kù)操作
- php下mysql數(shù)據(jù)庫(kù)操作類(改自discuz)
- PHP數(shù)據(jù)庫(kù)調(diào)用類調(diào)用實(shí)例(詳細(xì)注釋)
- PHP輕量級(jí)數(shù)據(jù)庫(kù)操作類Medoo增加、刪除、修改、查詢例子
- PHP實(shí)現(xiàn)的MongoDB數(shù)據(jù)庫(kù)操作類分享
- PHP實(shí)現(xiàn)的sqlite數(shù)據(jù)庫(kù)連接類
- php數(shù)據(jù)庫(kù)操作model類(使用__call方法)
相關(guān)文章
提交表單后 PHP獲取提交內(nèi)容的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇提交表單后 PHP獲取提交內(nèi)容的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
PHP命令空間namespace及use的用法小結(jié)
命名空間一個(gè)最明確的目的就是解決重名問(wèn)題,PHP中不允許兩個(gè)函數(shù)或者類出現(xiàn)相同的名字,否則會(huì)產(chǎn)生一個(gè)致命的錯(cuò)誤。這篇文章主要介紹了PHP命令空間namespace及use的用法實(shí)踐總結(jié),需要的朋友可以參考下2017-11-11
LotusPhp筆記之:基于ObjectUtil組件的使用分析
學(xué)習(xí)要先易后難,好吧,我剛開始學(xué)習(xí)LotusPhp的時(shí)候,就是從最容易的Logger和ObjectUtil開始的,這2個(gè)組件基本沒(méi)有什么難度。一看就會(huì)2013-05-05
thinkPHP中_initialize方法實(shí)例分析
這篇文章主要介紹了thinkPHP中_initialize方法,結(jié)合實(shí)例形式分析了子類調(diào)用父類_initialize方法的原理與相關(guān)操作技巧,需要的朋友可以參考下2016-12-12
TP5框架實(shí)現(xiàn)的數(shù)據(jù)庫(kù)備份功能示例
這篇文章主要介紹了TP5框架實(shí)現(xiàn)的數(shù)據(jù)庫(kù)備份功能,結(jié)合實(shí)例形式分析了TP5數(shù)據(jù)庫(kù)備份功能相關(guān)原理及實(shí)現(xiàn)方法,需要的朋友可以參考下2020-04-04

