Copy Code code as follows:
<?php
/*
MYSQL Database Access Encapsulation class
MYSQL data access mode, PHP4 support process access with Mysql_, PHP5 start to support mysqli_ process and mysqli object-oriented
Access mode, this encapsulation class with Mysql_ encapsulation
General flow of data access:
1, Connection database mysql_connect or Mysql_pconnect
2, select Database mysql_select_db
3, execute SQL query mysql_query
4, processing the returned data mysql_fetch_array mysql_num_rows MYSQL_FETCH_ASSOC mysql_fetch_row etc
*/
Class Db_mysql
{
var $querynum = 0; The number of times the current page process queried the database
var $dblink; Database connection Resources
Link 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 to database!") ");
}
Set 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);
}
Execute SQL query
function query ($sql)
{
$this->querynum++;
Return mysql_query ($sql, $this->dblink);
}
Returns the number of record rows affected by the most recent insert,update or delete query associated with a connection handle
function Affected_rows ()
{
Return Mysql_affected_rows ($this->dblink);
}
Gets the number of rows in the result set, only valid for the result set of the select query
function Num_rows ($result)
{
Return mysql_num_rows ($result);
}
Get the query results for a single grid
function result ($result, $row =0)
{
Return mysql_result ($result, $row);
}
Gets the ID generated by the previous INSERT operation, only for operations where the Auto_increment ID is available 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 the form of an associative array represented by a number key
function Fetch_row ($result)
{
Return mysql_fetch_row ($result);
}
Extracts the current row from the result set, returning the form as an associative array of the field name key
function Fetch_assoc ($result)
{
Return Mysql_fetch_assoc ($result);
}
Extracts the current row from the result set, returning in the form of an associative array of field names and numbers as key
function Fetch_array ($result)
{
return mysql_fetch_array ($result);
}
Close link
function Close ()
{
Return Mysql_close ($this->dblink);
}
Output a simple error HTML message and terminate the program
function Halt ($msg)
{
$message = "$message. = "<meta content= ' text/html;charset=gb2312 ' >\n";
$message. = "$message. = "<body>\n";
$message. = "Database error:". Htmlspecialchars ($msg). \ n ";
$message. = "</body>\n";
$message. = "Echo $message;
Exit;
}
}
?>