<?php
/*
* 这个类本来是phplib里的一部分,我做了些修改。
*/
class mydb_sql {
var $host = "";
var $database = "";
var $user = "";
var $password = "";
var $link_id = 0;
var $query_id = 0;
var $record = array();
var $row;
var $errno = 0;
var $error = "";
var $auto_free = 0; ## set this to 1 for automatic mysql_free_result()
var $auto_commit = 0; ## set this to 1 to automatically commit results
var $debugmode = 0;
function connect() {
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);
}
}
}
function query($query_string) {
$this->connect();
if ($this->debugmode)
printf("debug: query = %s<br>/n", $query_string);
$this->query_id = mysql_query($query_string,$this->link_id);
$this->row = 0;
$this->errno = mysql_errno();
$this->error = mysql_error();
if (!$this->query_id) {
$this->halt("invalid sql: ".$query_string);
}
return $this->query_id;
}
function next_record() {
$this->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);
if ($status)
$this->row = $pos;
return;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$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);
for ($i=0; $i<$count; $i++) {
$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;
}
mysql_free_result($id);
return $res;
}
function affected_rows() {
return mysql_affected_rows($this->link_id);
}
function num_rows() {
return mysql_num_rows($this->query_id);
}
function num_fields() {
return mysql_num_fields($this->query_id);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($name) {
return $this->record[$name];
}
function p($name) {
print $this->record[$name];
}
function halt($msg) {
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.");
}
}
class sql extends mydb_sql {
var $host = "localhost";
var $database = "";
var $user = "";
var $password = "";
function free_result() {
return @mysql_free_result($this->query_id);
}
function rollback() {
return 1;
}
function commit() {
return 1;
}
function autocommit($onezero) {
return 1;
}
function insert_id($col="",$tbl="",$qual="") {
return mysql_insert_id($this->query_id);
}
}
?>
新闻热点
疑难解答