时间:2021-07-01 10:21:17 帮助过:33人阅读
class db_mysql { private $db; private $dbhost;//数据库主机名 private $dbuser;//数据库用户名 private $dbpw;//数据库密码 private $dbname;//数据库名字 private $pconnect;//0 =>不持久连接, 1=> 持久连接 private $charset;//数据库编码 var $query_num=0;//查询次数 public function __construct($dbhost,$dbuser,$dbpw,$dbname,$pconnect=0,$charset="utf8") { $this->dbhost=$dbhost; $this->dbuser=$dbuser; $this->dbpw=$dbpw; $this->dbname=$dbname; $this->pconnect=$pconnect; $this->charset=$charset; $this->connect();//初始化数据库链接 } //数据库链接 private function connect() { if (!function_exists('mysql_connect')||!function_exists('mysql_pconnect')){ $this->halt('服务器PHP不支持MySql数据库!'); } if(!empty($this->dbhost)|| !empty($this->dbuser)||$this->dbhost!=="" ||$this->dbuser!=="") { if($this->pconnect==1){ $this->db=@mysql_pconnect($this->dbhost,$this->dbuser,$this->dbpw,true); if(!$this->db){ $this->halt("数据库持久连接失败,可能是数据库用户名或密码错误!"); } } else { $this->db=@mysql_connect($this->dbhost,$this->dbuser,$this->dbpw); if(!$this->db){ $this->halt("数据库连接失败,可能是数据库用户名或密码错误!"); } } } if ($this->version() >'4.1'){ mysql_query("set names ".$this->charset,$this->db); } if(!empty($this->dbname)||$this->dbname!=""){ if(!mysql_select_db($this->dbname,$this->db)){ $this->halt("数据库选择失败,可能是数据库名错误!"); } } } //查询执行 function query($sql, $unbuffered = false) { $func = $unbuffered ? 'mysql_unbuffered_query' : 'mysql_query'; $query = @$func($sql); if(!$query){ $this->halt('Query 错误 '.$sql); } $this->query_num++; return $query; } //选择数据库 function select_db($dbname) { return mysql_select_db($dbname, $this->db); } //取单一数据(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH) function fetch($query, $result_type=MYSQL_ASSOC) { return mysql_fetch_array($query, $result_type); } //取结果集数据(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH) function fetch_array($query, $result_type=MYSQL_BOTH) { return mysql_fetch_array($query, $result_type); } function fetch_row($query) { return mysql_fetch_row($query); } function fetch_field($query) { return mysql_fetch_field($query); } function fetch_assoc($query) { return mysql_fetch_assoc($squery); } //返回根据所取得的行生成的对象* function fetch_object($result) { return mysql_fetch_object($result); } //返回上次UPDATE更改的行数,上次DELETE删除的行数,或上次INSERT语句插入的行数。 function affected_rows() { return mysql_affected_rows($this->db); } //返回结果集 function result($query, $row = 0, $field = 0) { return @mysql_result($query, $row, $field); } //释放结果集 function free_result($query) { return mysql_free_result($query); } //返回行数 function num_rows($query) { return mysql_num_rows($query); } //获得结果集中字段的数目 function num_fields($query) { return mysql_num_fields($query); } //返回插入的ID值 function insert_id() { return ($id = mysql_insert_id($this->db)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0); } //错误信息 function error() { return (($this->db) ? mysql_error($this->db) : mysql_error()); } //错误代码 function errno() { return intval(($this->db) ? mysql_errno($this->db) : mysql_errno()); } //获取数据库版本 function version() { if(empty($this->version)) { $this->version = mysql_get_server_info($this->db); } return $this->version; } //列出 MySQL 数据库中的表 function get_table_names($dbname='') { if(empty($dbname)) { $db_name=$this->dbname; } else { $db_name=$dbname; } $result = mysql_list_tables($db_name); $num_tables = @mysql_numrows($result); for ($i = 0; $i < $num_tables; $i++) { $tables[] = mysql_tablename($result, $i); } mysql_free_result($result); return $tables; } function data_seek($result, $offset) { return mysql_data_seek($result, $offset); } // 对特殊字符进行过滤value 值 function escape_string($str){ return mysql_real_escape_string($str,$this->db) or mysql_real_escape_string($str); } //关闭数据库 function close() { return mysql_close($this->db); } // function get_client_info() { return mysql_get_client_info($this->db); } // function get_proto_info() { return mysql_get_proto_info($this->db); } // function get_host_info() { return mysql_get_host_info($this->db); } // function field_len($result, $i) { return mysql_field_len($result, $i); } // function field_name($result, $i) { return mysql_field_name($result, $i); } // function field_flags($result, $i) { return mysql_field_flags($result, $i); } // function get_fields_meta($result) { $fields = array(); $num_fields = mysql_num_fields($result); for ($i = 0; $i < $num_fields; $i++) { $field = mysql_fetch_field($result, $i); $field->flags = mysql_field_flags($result, $i); $field->orgtable = mysql_field_table($result, $i); $field->orgname = mysql_field_name($result, $i); $fields[] = $field; } return $fields; } //错误提示 private function halt($msg='') { $s=''; $error = $this->error(); $errorno = $this->errno(); $s.=""; if($this->db){ $Version=$this->version(); $s.="".$Version." "; } $s.="".$msg." "; $s.="".$error." "; $s.="".$errorno." "; die($s); } }