对一个数据库类的分析
时间:2007-12-28 来源:061107
数据库类的分析
Link_ID ) { //如果
[url=javascript:;]数据库[/url]
连接号为0,即没有连接
$this->Link_ID=@mysql_connect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID) {
$this->halt("Link-ID == false, connect failed");
}
if (!mysql_query(sprintf("use %s",$this->Database),$this->Link_ID)) { //结果为mysql_query(use xxxdatabase,linkid), 即mysql_select_db...)
$this->halt("cannot use database ".$this->Database);
}
}
}
function connect_check() {
$this->Link_ID=mysql_connect($this->Host, $this->User, $this->Password);
if($this->Link_ID) {
if(mysql_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
return true;
}else
return false;
}else
return false;
}
function close(){ //关闭数据库
if ( 0 != $this->Link_ID ) {
mysql_close($this->Link_ID);
}
}
function query($Query_String) { //数据库查询
$this->Query_ID = mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
if($this->Errno === 1062) return "-1062"; //程序验证1062错误必须用===三元运算符; 1062错误:数据库中无法插入相同数据,用于避免相同用户名注册.类似于先select后判断用户名是否为空,但比后者效率高
$this->Error = $this->Database;
if (!$this->Query_ID) { //如果sql语句出错
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
function result($Query_String) { //返回sql结果(只返回第一条记录)
$this->Query_ID = mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
if($this->Errno === 1062) return "-1062";
$this->Error = $this->Database;
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}
$result = @ mysql_fetch_array($this->Query_ID); //mysql_fetch_array:从结果集中取得一行作为关联数组,或数字数组,或二者兼有
if(!$result) return false;
@ mysql_free_result($this->Query_ID);
return (count($result)Record = mysql_fetch_array($this->Query_ID);
$this->Row += 1;
# $this->Errno = mysql_errno();
# $this->Error = mysql_error();
$stat = is_array($this->Record);
if (!$stat && $this->Auto_free) { //如果没释放结果内存,且没求得结果,则释放
mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}
function seek($pos) {//根据结果集的指针定位记录的位置
$status = mysql_data_seek($this->Query_ID, $pos); //mysql_data_seek() 将指定的结果标识所关联的
[url=javascript:;]MySQL[/url]
结果内部的行指针移动到指定的行号
if ($status)
$this->Row = $pos;
return;
}
function metadata($table) { //根据结果集建立表数组
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = @mysql_list_fields($this->Database, $table); //mysql_list_fields 列出 MySQL 结果中的字段
if ($id Errno = mysql_errno();
# $this->Error = mysql_error();
$this->halt("Metadata query failed.");
}
$count = mysql_num_fields($id);
for ($i=0; $iDatabase error: %s
\n", $msg);
printf("MySQL Error: %s (%s)
\n",
$this->Errno,
$this->Error);
die("Session halted.");
}
}
?>
相关阅读 更多 +