| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 | 
							- <?php
 
- 	/**
 
- 	* 数据库操作类
 
- 	*/
 
- 	class MysqlHandler {
 
- 		private $host;          // 主机地址
 
- 		private $port;          // 端口
 
- 		private $user;          // 授权用户
 
- 		private $pwd;           // 密码
 
- 		private $database;      // 数据库名称
 
- 		private $db_charset;    // 数据库字符集
 
- 		private $conn;          // 数据库连接资源
 
- 		//构造函数,与类名同名
 
- 		/**
 
- 		* 参数$conf 是配置数组,通过配置,初始化并创建一个数据库连接
 
- 		*/
 
- 		public function MysqlHandler($conf) {
 
- 				$this->host = $conf['host'];
 
- 				$this->port = $conf['port'];
 
- 				$this->user = $conf['user'];
 
- 				$this->pwd  = $conf['pwd'];
 
- 				$this->database   = $conf['db_name'];
 
- 				if(empty($conf['db_charset'])){
 
- 					$this->db_charset = 'utf8';
 
- 				} else {
 
- 					$this->db_charset = strtolower($conf['db_charset']);
 
- 				}
 
- 				$this->init_connect();
 
- 				$this->select_db();
 
- 		}
 
- 		/**
 
- 		*	初始化数据库连接 无返回值
 
- 		*/
 
- 		private function init_connect() {
 
- 			$server     = $this->host.":".$this->port;
 
- 			$this->conn = mysqli_connect($server, $this->user, $this->pwd,$this->database);
 
- 			if (mysqli_connect_errno($this->conn))
 
- 			{
 
- 				echo "连接 MySQL 失败: " . mysqli_connect_error();
 
- 			}
 
- 		}
 
- 		/**
 
- 		*	选择要访问的数据库
 
- 		*/
 
- 		private function select_db() {
 
- 			$set_sql    = "set character set ".$this->db_charset;
 
- 			mysqli_query($this->conn,$set_sql);
 
- 			$set_sql    = " set names ".$this->db_charset;
 
- 			mysqli_query($this->conn,$set_sql);
 
- 		}
 
- 		/**
 
- 		*	执行查询语句。返回结果集二维数组
 
- 		*  SELECT,SHOW,EXPLAIN 或 DESCRIBE
 
- 		*/
 
- 		public function query($sql) {
 
- 			$rs = mysqli_query ($this->conn,$sql);
 
- 			//$this->save_log($sql);
 
- 			if(mysqli_num_rows($rs)>0) {
 
- 				while ($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {
 
- 					$records_arr[] = $row;
 
- 				}
 
- 				return $records_arr;
 
- 			} else {
 
- 				return null;
 
- 			}
 
- 		}
 
- 		/**
 
- 		 * 查询单个记录的第一列,例如
 
- 		 * @param string $sql
 
- 		 */
 
- 		public function query_first_column($sql) {
 
- 			
 
- 			if(strrpos(strtolower($sql),'union')!== false){
 
- 				Log::save_run_log($sql, 'hacker');
 
- 			} 
 
- 			$rs = mysqli_query ($this->conn,$sql);
 
- 			if(mysqli_num_rows($rs)>0) {
 
- 				if ($row = mysqli_fetch_array($rs, MYSQL_ASSOC)) {
 
- 					foreach ($row as $key => $val){
 
- 						return $val;
 
- 					}
 
- 					return null;
 
- 				} else {
 
- 					return null;
 
- 				}
 
- 			} else {
 
- 				return null;
 
- 			}
 
- 		}
 
- 		/**
 
- 		* 执行数据库操作脚本语句 返回成功或失败 : TRUE OR FALSE
 
- 		* update
 
- 		*/
 
- 		public function execute_sql($sql){
 
- 			return $this->exe_sql_and_log($sql);
 
- 		}
 
- 		/**
 
- 		* 执行插入操作返回数据id或失败 : ID OR FALSE
 
- 		* insert
 
- 		*/
 
- 		public function insert_sql($sql){
 
- 			$rs = $this->exe_sql_and_log($sql);
 
- 			if($rs){
 
- 				$id = mysqli_insert_id($this->conn);
 
- 				return  $id;
 
- 			}else{
 
- 				return  false;
 
- 			}
 
- 		}
 
- 		/**
 
- 		* 返回成功或失败 : TRUE OR FALSE
 
- 		*/
 
- 		private function exe_sql_and_log($sql) {
 
- 			
 
- 			$rs = mysqli_query($this->conn,$sql);
 
- 			$this->save_log($sql);
 
- 			if($rs){
 
- 				return true;
 
- 			}else{
 
- 				return false;
 
- 			}
 
- 		}
 
- 		/**
 
- 		*  tostring 方法,查看本实例的连接参数。
 
- 		*/
 
- 		public function to_string(){
 
- 			return "host:$this->host|port:$this->port|user:$this->user|pwd:$this->pwd|database:$this->database|chartset:$this->db_charset <br>";
 
- 		}
 
- 		
 
- 		public function save_log($sql){
 
- 			$info  = mysqli_errno($this->conn) . "|$sql|" . mysqli_error($this->conn);
 
- 			Log::save_run_log($info, 'db');
 
- 		}
 
- 		
 
- 		public function close(){
 
- 			mysqli_close($this->conn);
 
- 		}
 
- 	}
 
- ?>
 
 
  |