Database connection operation class
Release: 2016-07-25 09:11:41
Original
750 people have browsed it
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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31