操作MYSQL的一个PHP类
来源:互联网 发布:手机号注册查询软件 编辑:程序博客网 时间:2024/05/18 00:46
<?PHP var $Link_ID = 0; var $Errno = 0; var $Auto_free = 0; ## Set this to 1 for automatic MYSQL_free_result() var $debugmode = 0; function connect() { function query($Query_String) { if ($this->debugmode) $this->Query_ID = MYSQL_query($Query_String,$this->Link_ID); return $this->Query_ID; function next_record() { $stat = is_array($this->Record); function seek($pos) { function metadata($table) { $this->connect(); for ($i=0; $i<$count; $i++) { MYSQL_free_result($id); function affected_rows() { function num_rows() { function num_fields() { function nf() { function np() { function f($Name) { function p($Name) { function halt($msg) { class SQL extends MyDB_Sql { function free_result() { function rollback() { function commit() { function autocommit($onezero) { function insert_id($col="",$tbl="",$qual="") {
/*
* 这个类本来是PHPLIB里的一部分,我做了些修改。
*/
class MyDB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Query_ID = 0;
var $Record = array();
var $Row;
var $Error = "";
var $Auto_commit = 0; ## set this to 1 to automatically commit results
if ( 0 == $this->Link_ID ) {
$this->Link_ID=MYSQL_pconnect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID) {
$this->halt("Link-ID == false, pconnect failed");
}
if (!MYSQL_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
}
}
}
$this->connect();
printf("Debug: query = %s<br>/n", $Query_String);
$this->Row = 0;
$this->Errno = MYSQL_errno();
$this->Error = MYSQL_error();
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}
}
$this->Record = MYSQL_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = MYSQL_errno();
$this->Error = MYSQL_error();
if (!$stat && $this->Auto_free) {
MYSQL_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}
$status = MYSQL_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
return;
}
$count = 0;
$id = 0;
$res = array();
$id = @MYSQL_list_fields($this->Database, $table);
if ($id < 0) {
$this->Errno = MYSQL_errno();
$this->Error = MYSQL_error();
$this->halt("Metadata query failed.");
}
$count = MYSQL_num_fields($id);
$res[$i]["table"] = MYSQL_field_table ($id, $i);
$res[$i]["name"] = MYSQL_field_name ($id, $i);
$res[$i]["type"] = MYSQL_field_type ($id, $i);
$res[$i]["len"] = MYSQL_field_len ($id, $i);
$res[$i]["flags"] = MYSQL_field_flags ($id, $i);
$res["meta"][$res[$i]["name"]] = $i;
$res["num_fields"]= $count;
}
return $res;
}
return MYSQL_affected_rows($this->Link_ID);
}
return MYSQL_num_rows($this->Query_ID);
}
return MYSQL_num_fields($this->Query_ID);
}
return $this->num_rows();
}
print $this->num_rows();
}
return $this->Record[$Name];
}
print $this->Record[$Name];
}
printf("</td></tr></table><b>Database error:</b> %s<br>/n", $msg);
printf("<b>MYSQL Error</b>: %s (%s)<br>/n",
$this->Errno,
$this->Error);
die("Session halted.");
}
}
var $Host = "localhost";
var $Database = "";
var $User = "";
var $Password = "";
return @MYSQL_free_result($this->Query_ID);
}
return 1;
}
return 1;
}
return 1;
}
return MYSQL_insert_id($this->Query_ID);
}
}
?>