MYSQL data access method. php4 supports the process access method starting with mysql _. php5 supports the process starting with mysqli _ and the object-oriented access method of mysqli. This encapsulation class is encapsulated with mysql _.
The code is as follows:
/*
MYSQL database access encapsulation
MYSQL data access method. php4 supports process access methods starting with mysql _. php5 supports processes starting with mysqli _ and mysqli object-oriented
Access Method. This encapsulation class is encapsulated by mysql _.
General data access process:
1. connect to the database mysql_connect or mysql_pconnect
2. select mysql_select_db.
3. execute SQL query mysql_query
4. process the returned data mysql_fetch_array mysql_num_rows mysql_fetch_assoc mysql_fetch_row etc.
*/
Class db_mysql
{
Var $ querynum = 0; // Number of times the process on the current page queries the database
Var $ dblink; // database connection resource
// Link to the 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 ("The database cannot be connected! ");
}
// Set the query character set
Mysql_query ("SET character_set_connection = {$ dbcharset}, character_set_results = {$ dbcharset}, character_set_client = binary", $ this-> dblink );
// Select a database
$ Dbname & @ mysql_select_db ($ dbname, $ this-> dblink );
}
// Select a 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 rows affected by the last INSERT, UPDATE, or DELETE statement associated with the connection handle query.
Function affected_rows ()
{
Return mysql_affected_rows ($ this-> dblink );
}
// Obtain the number of rows in the result set, which is only valid for the result set of the select query
Function num_rows ($ result)
{
Return mysql_num_rows ($ result );
}
// Obtain the query result of a single cell
Function result ($ result, $ row = 0)
{
Return mysql_result ($ result, $ row );
}
// Obtain the ID generated by the previous INSERT operation, which is valid only for operations with AUTO_INCREMENT ID on the table
Function insert_id ()
{
Return ($ id = mysql_insert_id ($ this-> dblink)> = 0? $ Id: $ this-> result ($ this-> query ("SELECT last_insert_id ()"), 0 );
}
// Extract the current row from the result set and return the result in the form of an associated array represented by the number as the key
Function fetch_row ($ result)
{
Return mysql_fetch_row ($ result );
}
// Extract the current row from the result set and return the result in the form of an associated array with the field name as the key
Function fetch_assoc ($ result)
{
Return mysql_fetch_assoc ($ result );
}
// Extract the current row from the result set and return the result in the form of an associated array represented by the field name and number as the key
Function fetch_array ($ result)
{
Return mysql_fetch_array ($ result );
}
// Close the link
Function close ()
{
Return mysql_close ($ this-> dblink );
}
// Output simple error html prompt information and terminate the program
Function halt ($ msg)
{
$ Message ="\ N\ N ";
$ Message. =" \ N ";
$ Message. ="\ N ";
$ Message. ="\ N ";
$ Message. = "database error:". htmlspecialchars ($ msg). "\ n ";
$ Message. ="\ N ";
$ Message. ="";
Echo $ message;
Exit;
}
}
?>