PHP accessing MYSQL database encapsulation

Source: Internet
Author: User
Tags pconnect
& Lt ;? 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 methods, this encapsulation class uses mysql _ to encapsulate the general process of data access: 1. connect to the database mysql_connectormysql_pconnect2, Select
 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 database $ dbname & @ mysql_select_db ($ dbname, $ this-> dblink);} // select database function select_db ($ dbname) {return mysql_select_db ($ dbname, $ this-> dblink);} // execute the SQL query function query ($ SQL) {$ this-> querynum ++; return mysql_query ($ SQL, $ this-> dblink);} // return Returns the last number of rows affected by the INSERT, UPDATE, or DELETE query associated with the connection handle function affected_rows () {return mysql_affected_rows ($ this-> dblink );} // Obtain the number of rows in the result set. function num_rows ($ result) {return mysql_num_rows ($ result) is valid only for the result set of the select query );} // Obtain the query result function result ($ result, $ row = 0) {return mysql_result ($ result, $ row);} // obtain the ID generated by the previous INSERT operation, 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, returns the function fetch_row ($ result) {return mysql_fetch_row ($ result) ;}// extracts the current row from the result set in the form of an associated array represented by the key, return function fetch_assoc ($ result) {return mysql_fetch_assoc ($ result);} // extract the current row from the result set in the form of an associated array represented by the field name as key, returns the function fetch_array ($ result) {return mysql_fetch_array ($ result);} // closes the link function close () in the form of an associated array represented by the field name and number as the key () {return mysql_close ($ this-> dblink);} // output a simple error html prompt 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 ;}}?>

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.