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) ;}}?>