Home  >  Article  >  php教程  >  php数据库连接类

php数据库连接类

PHP中文网
PHP中文网Original
2016-05-25 17:15:091162browse

[PHP]代码  

<?php  
	/*
	* 名称: 数据库链接类
	* 介绍: 适用于各种数据库链接
	* 作者: hetaoren <452649510@qq.com>
	* 创建时间: 2010-12-07
	* 最后修改: 2010-12-08
	*/

	class mysql {  
		private $_link;  
		
		public function __construct($dbhost=&#39;localhost&#39;,$dbuser=&#39;root&#39;,$dbpassword=&#39;&#39;,$dbname=&#39;taojindidai&#39;,$charset=&#39;gbk&#39;) {  
			$this->_link = mysql_connect($dbhost,$dbuser,$dbpassword,true);  /*连接数据库*/ 
			$this->_link or $this->errmsg(&#39;无法连接MYSQL服务器!&#39;);  /*是否连接成功*/ 
			if ($this->version() > &#39;4.1&#39;) {                  /*检查数据库版本*/ 
				$this->query(&#39;set names &#39;.$charset);         /*设置数据库编码*/ 
			}  
			/*打开数据库*/
			mysql_select_db($dbname,$this->_link) or $this->errmsg(&#39;无法连接数据库!&#39;);  
		}  
		
		/*执行数据库操作*/ 
		public function query($sql)	{                                                               
			$result = mysql_query($sql,$this->_link);  
			$result or $this->errmsg(&#39;执行SQL语句错误!&#39;);  
			return $result;  
		} 
		 
		/*返回根据从结果集取得的行生成的数组*/ 
		/*MYSQL_BOTH 得到一个同时包含关联和数字索引的数组 (如同 mysql_fetch_array())*/ 
		/*MYSQL_ASSOC 得到一个同时包含关联和数字索引的数组 (如同 mysql_fetch_assoc())*/ 
		/*MYSQL_NUM 得到一个同时包含关联和数字索引的数组 (如同 mysql_fetch_row())*/ 
		public function fetch_array($result,$type = MYSQL_ASSOC) {
			return mysql_fetch_array($result,$type);     
		} 
		 
		/*返回根据所取得的行生成的对象*/ 
		public function fetch_object($result) {  
			return mysql_fetch_object($result);  
		} 
		 
		/*取得前一次 MySQL 操作所影响的记录行数*/ 
		public function affected_rows() {  
			return mysql_affected_rows($this->_link);  
		}  
		
		/* 释放结果内存*/ 
		public function free_result($result) {  
			return mysql_free_result($result);   
		}  
		
		/* 取得结果集中行的数目*/ 
		public function num_rows($result) {  
			return mysql_num_rows($result);  
		}
		  
		/* 取得结果集中字段的数目*/ 
		public function num_fields($result) {  
			return mysql_num_fields($result);  
		}  
		
		/*取得上一步 INSERT 操作产生的 ID*/ 
		public function insert_id() {  
			return mysql_insert_id($this->_link);  
		} 
		 
		/* 发出mysql执行错误*/ 
		private function errmsg($msg) {  
			$message  = &#39;<strong>一个MySQL错误发生!</strong><br />&#39;;  
			$message .= &#39;<strong>错误号:</strong>&#39;. mysql_errno($this->_link) .&#39;<br />&#39;;  
			$message .= &#39;<strong>错误描述:</strong>&#39;. $msg . mysql_error($this->_link) .&#39;<br />&#39;;  
			$message .= &#39;<strong>错误时间:</strong>&#39;. date(&#39;Y-m-d H:i:s&#39;);  
			exit($message);  
		} 
		 
		/*返回连接的标识*/ 
		public function link_id() {  
			return $this->_link;  
		}
		  
		/*返回数据库服务器版本*/ 
		public function version() {  
			return mysql_get_server_info($this->_link);  
		}  
		
		/*获得客户端真实的IP地址*/ 
		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(getenv("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[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], "unknown")) {  				$ip = "unknown";  

				$ip = $_SERVER[&#39;REMOTE_ADDR&#39;];  
			}else{  
			}  
			return ($ip);  
		}  
	} 
?>

                   

                   

Statement:
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