PHP accessing MYSQL database encapsulation class (with function description)

Source: Internet
Author: User
Tags pconnect

Copy codeThe Code is as follows: <? Php
/*
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 = "$ 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;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.