Database connection operation class
- class mysql
- {
- private $_link;
-
- public function __construct($dbhost,$dbuser,$dbpassword,$dbname,$charset='utf8')
- {
- $ this->_link = mysql_connect($dbhost,$dbuser,$dbpassword,true); /*Connect to database*/
- $this->_link or $this->errmsg('Can't connect to MySQL server! '); /*Whether the connection is successful*/
- if ($this->version() > '4.1') { /*Check the database version*/
- $this->query('set names '.$charset ); /*Set database encoding*/
- }
- mysql_select_db($dbname,$this->_link) or $this->errmsg('Can't select the database!'); /*Open the database*/
- }
-
-
- /*Perform database operations*/
- public function query($sql)
- {
-
- $result = mysql_query($sql,$this->_link);
- $result or $this->errmsg( 'Execute sql sentence error!');
- return $result;
- }
-
-
- /*Returns an array generated based on the rows obtained from the result set*/
- /*MYSQL_BOTH Gets an array containing both associative and numeric indexes (like mysql_fetch_array())*/
- /*MYSQL_ASSOC Gets an array that contains both associative and numeric indexes (like mysql_fetch_assoc())*/
- /*MYSQL_NUM Gets an array that contains both associative and numeric indexes (like mysql_fetch_row())*/
- public function fetch_array($result,$type = MYSQL_BOTH)
- {
- return mysql_fetch_array($result,$type);
- }
-
- /*Return the object generated based on the obtained row*/
- public function fetch_object($result )
- {
- return mysql_fetch_object($result);
- }
-
- /*Get the number of record rows affected by the previous MySQL operation*/
- public function affected_rows()
- {
- return mysql_affected_rows($this->_link);
- }
-
- /* Release the 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);
- }
-
-
- /* Get the number of fields in the result set*/
- public function num_fields($result)
- {
- return mysql_num_fields($result);
- }
-
- /* Get the ID generated by the previous INSERT operation*/
- public function insert_id()
- {
- return mysql_insert_id($this->_link);
- }
-
- /* Issue a mysql execution error*/
- private function errmsg($msg)
- {
- $message = 'A mysql error has occurred!
';
- $message .= 'Error Number:'. 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 the connection ID*/
- public function link_id()
- {
- return $this->_link;
- }
-
-
- /*Return the database server version*/
- public function version() {
- return mysql_get_server_info($this->_link);
- }
-
-
- /*Get 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");
- } else
- if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
- $ ip = getenv("HTTP_X_FORWARDED_FOR");
- } else
- if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
- $ip = getenv("REMOTE_ADDR");
- } else
- if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
- $ip = $_SERVER['REMOTE_ADDR'] ;
- } else {
- $ip = "unknown";
- }
- return ($ip);
- }
-
-
- }
Copy code
|