<?php
class db_mysql
{
var $querynum = 0 ; //The number of times the current page process queries the database
var $dblink ; //Database connection resources
//Linked database
function connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0 , $halt=true)
{
$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect' ;
$this->dblink = @$func($dbhost,$dbuser,$dbpw) ;
if ($halt && !$this->dblink)
{
$this->halt(" Unable to link database! ");
}
//Set the query character set
mysql_query("SET character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this->dblink) ;
//Select database
$dbname && @mysql_select_db($dbname,$this->dblink) ;
}
//Select database
function select_db($dbname)
{
return mysql_select_db($dbname,$this->dblink);
}
//Executing SQL queries
function query($sql)
{
$this->querynum++ ;
return mysql_query($sql,$this->dblink) ;
}
//Returns the number of rows of records affected by the last INSERT, UPDATE, or DELETE query associated with the connection handle
function affected_rows()
{
return mysql_affected_rows($this->dblink) ;
}
//Gets the number of rows in the result set, valid only for the result set of the select query
function num_rows($result)
{
return mysql_num_rows($result) ;
}
//Gets the result of a single-cell query
function result($result,$row=0)
{
return mysql_result($result,$row) ;
}
//Gets the ID generated by the previous INSERT operation, valid only for operations with AUTO_INCREMENT ID for the table
function insert_id()
{
return ($id = mysql_insert_id($this->dblink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
//Extracts the current row from the result set and returns it as an associative array with a number as the key
function fetch_row($result)
{
return mysql_fetch_row($result) ;
}
//The current row is extracted from the result set and returned as an associative array whose field name is key
function fetch_assoc($result)
{
return mysql_fetch_assoc($result);
}
//Extracts the current row from the result set and returns it as an associative array with the field name and number as the key
function fetch_array($result)
{
return mysql_fetch_array($result);
}
//Close links
function close()
{
return mysql_close($this->dblink) ;
}
//Output a simple HTML error message and terminate the program
function halt($msg)
{
$message = "<html>n<head>n" ;
$message .= "<meta content='text/html;charset=gb2312'>n" ;
$message .= "</head>n" ;
$message .= "<body>n" ;
$message .= " Database error: ".htmlspecialchars($msg)."n" ;
$message .= "</body>n" ;
$message .= "</html>" ;
echo $message ;
exit ;
}
}
?>