|  | 
 
| 
 
 
 
 
 
 
 
 
 | 010 | public function __construct() { | 
 
 | 011 | $this->time = $this->microtime_float(); | 
 
 | 012 | require_once("config.db.php"); | 
 
 | 013 | $this->connect($db_config["hostname"], $db_config["username"], $db_config["password"], $db_config["database"], $db_config["pconnect"]); | 
 
 | 014 | $this->is_log = $db_config["log"]; | 
 
 
 | 016 | $handle = fopen($db_config["logfilepath"]."dblog.txt", "a+"); | 
 
 | 017 | $this->handle=$handle; | 
 
 
 
 
 
 | 022 | public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset='utf8') { | 
 
 
 | 024 | $this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true); | 
 
 
 | 026 | $this->halt("数据库连接失败"); | 
 
 
 
 | 029 | $this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw); | 
 
 
 | 031 | $this->halt("数据库持久连接失败"); | 
 
 
 
 | 034 | if(!@mysql_select_db($dbname,$this->link_id)) { | 
 
 | 035 | $this->halt('数据库选择失败'); | 
 
 
 | 037 | @mysql_query("set names ".$charset); | 
 
 
 
 
 | 041 | public function query($sql) { | 
 
 | 042 | $this->write_log("查询 ".$sql); | 
 
 | 043 | $query = mysql_query($sql,$this->link_id); | 
 
 | 044 | if(!$query) $this->halt('Query Error: ' . $sql); | 
 
 
 
 
 | 048 | //获取一条记录(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH) | 
 
 | 049 | public function get_one($sql,$result_type = MYSQL_ASSOC) { | 
 
 | 050 | $query = $this->query($sql); | 
 
 | 051 | $rt =& mysql_fetch_array($query,$result_type); | 
 
 | 052 | $this->write_log("获取一条记录 ".$sql); | 
 
 
 
 
 
 | 057 | public function get_all($sql,$result_type = MYSQL_ASSOC) { | 
 
 | 058 | $query = $this->query($sql); | 
 
 
 
 | 061 | while($row =& mysql_fetch_array($query,$result_type)) { | 
 
 
 
 
 | 065 | $this->write_log("获取全部记录 ".$sql); | 
 
 
 
 
 
 | 070 | public function insert($table,$dataArray) { | 
 
 
 
 | 073 | if( !is_array($dataArray) || count($dataArray)<=0) { | 
 
 | 074 | $this->halt('没有要插入的数据'); | 
 
 
 
 | 077 | while(list($key,$val)=each($dataArray)) { | 
 
 
 
 
 | 081 | $field = substr( $field,0,-1); | 
 
 | 082 | $value = substr( $value,0,-1); | 
 
 | 083 | $sql = "insert into $table($field) values($value)"; | 
 
 | 084 | $this->write_log("插入 ".$sql); | 
 
 | 085 | if(!$this->query($sql)) return false; | 
 
 
 
 
 
 | 090 | public function update( $table,$dataArray,$condition="") { | 
 
 | 091 | if( !is_array($dataArray) || count($dataArray)<=0) { | 
 
 | 092 | $this->halt('没有要更新的数据'); | 
 
 
 
 
 | 096 | while( list($key,$val) = each($dataArray)) | 
 
 | 097 | $value .= "$key = '$val',"; | 
 
 | 098 | $value .= substr( $value,0,-1); | 
 
 | 099 | $sql = "update $table set $value where 1=1 and $condition"; | 
 
 | 100 | $this->write_log("更新 ".$sql); | 
 
 | 101 | if(!$this->query($sql)) return false; | 
 
 
 
 
 
 | 106 | public function delete( $table,$condition="") { | 
 
 | 107 | if( empty($condition) ) { | 
 
 | 108 | $this->halt('没有设置删除的条件'); | 
 
 
 
 | 111 | $sql = "delete from $table where 1=1 and $condition"; | 
 
 | 112 | $this->write_log("删除 ".$sql); | 
 
 | 113 | if(!$this->query($sql)) return false; | 
 
 
 
 
 
 | 118 | public function fetch_array($query, $result_type = MYSQL_ASSOC){ | 
 
 | 119 | $this->write_log("返回结果集"); | 
 
 | 120 | return mysql_fetch_array($query, $result_type); | 
 
 
 
 
 | 124 | public function num_rows($results) { | 
 
 | 125 | if(!is_bool($results)) { | 
 
 | 126 | $num = mysql_num_rows($results); | 
 
 | 127 | $this->write_log("获取的记录条数为".$num); | 
 
 
 
 
 
 
 
 
 | 135 | public function free_result() { | 
 
 | 136 | $void = func_get_args(); | 
 
 | 137 | foreach($void as $query) { | 
 
 | 138 | if(is_resource($query) && get_resource_type($query) === 'mysql result') { | 
 
 | 139 | return mysql_free_result($query); | 
 
 
 
 | 142 | $this->write_log("释放结果集"); | 
 
 
 
 
 | 146 | public function insert_id() { | 
 
 | 147 | $id = mysql_insert_id($this->link_id); | 
 
 | 148 | $this->write_log("最后插入的id为".$id); | 
 
 
 
 
 
 | 153 | protected function close() { | 
 
 | 154 | $this->write_log("已关闭数据库连接"); | 
 
 | 155 | return @mysql_close($this->link_id); | 
 
 
 
 
 | 159 | private function halt($msg='') { | 
 
 | 160 | $msg .= "\r\n".mysql_error(); | 
 
 | 161 | $this->write_log($msg); | 
 
 
 
 
 
 | 166 | public function __destruct() { | 
 
 | 167 | $this->free_result(); | 
 
 | 168 | $use_time = ($this-> microtime_float())-($this->time); | 
 
 | 169 | $this->write_log("完成整个查询任务,所用时间为".$use_time); | 
 
 
 | 171 | fclose($this->handle); | 
 
 
 
 
 
 | 176 | public function write_log($msg=''){ | 
 
 
 | 178 | $text = date("Y-m-d H:i:s")." ".$msg."\r\n"; | 
 
 | 179 | fwrite($this->handle,$text); | 
 
 
 
 
 
 | 184 | public function microtime_float() { | 
 
 | 185 | list($usec, $sec) = explode(" ", microtime()); | 
 
 | 186 | return ((float)$usec + (float)$sec); | 
 
 
 
 
 [代码] config.db.php
 
 
 
 view source
 http:///js/syntax-highlighter-2.1.382/scripts/clipboard.swfprint?
 
 
 
 | 02 | $db_config["hostname"] = "localhost"; //服务器地址 | 
 
 | 03 | $db_config["username"] = "root"; //数据库用户名 | 
 
 | 04 | $db_config["password"] = "123"; //数据库密码 | 
 
 | 05 | $db_config["database"] = "test"; //数据库名称 | 
 
 | 06 | $db_config["charset"] = "utf8";//数据库编码 | 
 
 | 07 | $db_config["pconnect"] = 1;//开启持久连接 | 
 
 | 08 | $db_config["log"] = 1;//开启日志 | 
 
 | 09 | $db_config["logfilepath"] = './';//开启日志 | 
 
 | 
 | 
| 
 |