#102001/12/10 19:39:36
//Lee.Mars精心设计的DB类 Version 0.8.1 严禁修改^^
Class DB
{
var $connect_id; //数据库连接号
var $query_id; //mysql_query()返回的结果
var $info; //read()返回的数据
var $config; //数据库配置文件
var $debug = True ; //调试模块
var $stack = array() ; //数据堆栈 用于现场回复
var $point = 0; //堆栈指针
//DB类目前采用堆栈式存放法,不允许跳取
function error($num) //错误处理模块
{
if ($this->debug)
echo "出错模块:$mod_name
";
echo "DB 错误: ";
switch ($num)
{
case 0:
echo "不能连接MYSQL";
break;
case 1:
echo "不能使用数据库";
break;
case 2:
echo "配置文件不存在或文件名为空";
break;
case 3:
echo "不能关闭MYSQL连接";
break;
//支持错误号
default:
echo $num;
break;
//也支持字符串的错误信息
}
exit;
}
function DB($filename) //构造函数 设置配置文件
{
$this->mod_name = "DB"; //模块名,debug就靠他了
if (file_exists($filename) == False || empty($filename) == True)
//判断 1.文件是否存在 2.是否置空(因为空也算存在)
$this->error(2); //输出错误信息
else
$this->config = $filename;
}
function connectdb() //连接MYSQL
{
$this->mod_name = "connectdb";
include($this->config); //包含配置文件(不能用require)
$this->connect_id = @mysql_connect($DB_Host,$DB_User,$DB_Password) or $this->error(0); //进行连接
@mysql_select_db($DB_Name,$this->connect_id) or $this->error(1); //可选
}
function query($query) //发送SQL请求
{
$this->mod_name = "query";
$this->query_id = @mysql_query($query,$this->connect_id);
if ($this->query_id == False)
$this->error("ERROR ".mysql_errno().": ".mysql_error()."
"); //输出错误信息
}
function read()
{
$this->mod_name = "read";
if (!$this->query_id)
return False;
$this->info = @mysql_fetch_array($this->query_id); //读入$info中
return is_array($this->info); //返回$info是否是数组 否就是读完了
}
function close() //关闭连接
{
$this->mod_name = "close";
@mysql_close($this->connect_id) or $this->error(3);
}
function save() //保存现场
{
$this->mod_name = "save";
$this->point++;
$this->stack[$this->point][query_id] = $this->query_id;
$this->stack[$this->point][info] = $this->info;
}
function load() //读入现场
{
$this->mod_name = "load";
if (!$this->point)
return False;
$this->query_id = $this->stack[$this->point][query_id];
$this->info = $this->stack[$this->point][info];
$this->point--;
}
}
$DB = New DB("DBconfig.inc.php");
?>