Php database connection class-PHP source code

Source: Internet
Author: User
Database Connection

[PHP] code
 * Creation Time: 2010-12-07 * Last modification: 2010-12-08 */class mysql {private $ _ link; public function _ construct ($ dbhost = 'localhost', $ dbuser = 'root ', $ dbpassword = '', $ dbname = 'taojindidai ', $ charset = 'gbk') {$ this-> _ link = mysql_connect ($ dbhost, $ dbuser, $ dbpassword, true);/* connect to the database */$ this-> _ link or $ this-> errmsg ('The MYSQL server cannot be connected! ');/* Whether the connection is successful */if ($ this-> version ()>' 4. 1 ') {/* check the database version */$ this-> query ('set names '. $ charset);/* Set the database encoding */}/* open the database */mysql_select_db ($ dbname, $ this-> _ link) or $ this-> errmsg ('The database cannot be connected! ');}/* Perform database operations */public function query ($ SQL) {$ result = mysql_query ($ SQL, $ this-> _ link ); $ result or $ this-> errmsg ('SQL statement execution error! '); Return $ result ;} /* return the array generated based on the row obtained from the result set * // * MYSQL_BOTH gets an array containing both the join and numeric indexes (like mysql_fetch_array ()) * // * MYSQL_ASSOC obtains an array containing both join and numeric indexes (like mysql_fetch_assoc ()) * // * MYSQL_NUM get an array containing both join and numeric indexes (like mysql_fetch_row () */public function fetch_array ($ result, $ type = MYSQL_ASSOC) {return mysql_fetch_array ($ result, $ type);}/* returns the object generated based on the row obtained */public function fetch_object ($ result) {return mysql_fetch_object ($ result);}/* obtain the number of rows affected by the previous MySQL operation */public function affected_rows () {return mysql_affected_rows ($ this-> _ link);}/* release result memory */public function free_result ($ result) {return mysql_free_result ($ result );} /* get the number of rows in the result set */public function num_rows ($ result) {return mysql_num_rows ($ result );} /* obtain the number of fields in the result set */public function num_fields ($ result) {return mysql_num_fields ($ result );} /* obtain the ID generated by the previous INSERT operation */public function insert_id () {return mysql_insert_id ($ this-> _ link );} /* mysql execution error */private function errmsg ($ msg) {$ message ='A MySQL error occurred!
'; $ Message. ='Error code:'. Mysql_errno ($ this-> _ link ).'
'; $ Message. ='Error description:'. $ Msg. mysql_error ($ this-> _ link ).'
'; $ Message. ='Error Time:'. Date ('Y-m-d H: I: s'); exit ($ message);}/* return connection id */public function link_id () {return $ this-> _ link;}/* return Database Server version */public function version () {return mysql_get_server_info ($ this-> _ link );} /* obtain the real IP address of the client */function getip () {if (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown ")) {$ ip = getenv ("HTTP_CLIENT_IP");} elseif (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (gete Nv ("HTTP_X_FORWARDED_FOR"), "unknown") {$ ip = getenv ("HTTP_X_FORWARDED_FOR");} elseif (getenv ("REMOTE_ADDR ") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown") {$ ip = getenv ("REMOTE_ADDR ");} elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown") {$ ip = "unknown "; $ ip = $ _ SERVER ['remote _ ADDR '];} else {} return ($ ip) ;}}?>

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.