很好用的PHP數(shù)據(jù)庫類
更新時間:2009年05月27日 17:34:30 作者:
很好用的PHP數(shù)據(jù)庫類,三、四句代碼搞定一個表的操作,無論這個表字段有多復雜。
復制代碼 代碼如下:
<?
//很好用的PHP數(shù)據(jù)庫類,三、四句代碼搞定一個表的操作,無論這個表字段有多復雜。
//此類多次大量用在大型網(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ù)庫出錯!\");
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 \"寫數(shù)據(jù)到數(shù)據(jù)庫時出錯:\".$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ù)庫時出錯:\".$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 \"修改數(shù)據(jù)時出錯:\".$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ù)時出錯:\".$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ù)時出錯:\".$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實現(xiàn)的簡單數(shù)據(jù)庫操作Model類
- PHP簡單數(shù)據(jù)庫操作類實例【支持增刪改查及鏈式操作】
- 全新的PDO數(shù)據(jù)庫操作類php版(僅適用Mysql)
- PHP+Mysql樹型結(jié)構(gòu)(無限分類)數(shù)據(jù)庫設(shè)計的2種方式實例
- PHP數(shù)據(jù)庫操作之基于Mysqli的數(shù)據(jù)庫操作類庫
- php db類庫進行數(shù)據(jù)庫操作
- php下mysql數(shù)據(jù)庫操作類(改自discuz)
- PHP數(shù)據(jù)庫調(diào)用類調(diào)用實例(詳細注釋)
- PHP輕量級數(shù)據(jù)庫操作類Medoo增加、刪除、修改、查詢例子
- PHP實現(xiàn)的MongoDB數(shù)據(jù)庫操作類分享
- PHP實現(xiàn)的sqlite數(shù)據(jù)庫連接類
- php數(shù)據(jù)庫操作model類(使用__call方法)
相關(guān)文章
提交表單后 PHP獲取提交內(nèi)容的實現(xiàn)方法
下面小編就為大家?guī)硪黄峤槐韱魏?PHP獲取提交內(nèi)容的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05PHP命令空間namespace及use的用法小結(jié)
命名空間一個最明確的目的就是解決重名問題,PHP中不允許兩個函數(shù)或者類出現(xiàn)相同的名字,否則會產(chǎn)生一個致命的錯誤。這篇文章主要介紹了PHP命令空間namespace及use的用法實踐總結(jié),需要的朋友可以參考下2017-11-11LotusPhp筆記之:基于ObjectUtil組件的使用分析
學習要先易后難,好吧,我剛開始學習LotusPhp的時候,就是從最容易的Logger和ObjectUtil開始的,這2個組件基本沒有什么難度。一看就會2013-05-05TP5框架實現(xiàn)的數(shù)據(jù)庫備份功能示例
這篇文章主要介紹了TP5框架實現(xiàn)的數(shù)據(jù)庫備份功能,結(jié)合實例形式分析了TP5數(shù)據(jù)庫備份功能相關(guān)原理及實現(xiàn)方法,需要的朋友可以參考下2020-04-04