php数据库操作类
<?php |
002 |
Class DB { |
003 |
|
004 |
private $link_id; |
005 |
private $handle; |
006 |
private $is_log; |
007 |
private $time; |
008 |
|
009 |
//构造函数 |
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"]; |
015 |
if($this->is_log){ |
016 |
$handle = fopen($db_config["logfilepath"]."dblog.txt", "a+"); |
017 |
$this->handle=$handle; |
018 |
} |
019 |
} |
020 |
|
021 |
//数据库连接 |
022 |
public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset='utf8') { |
023 |
if( $pconnect==0 ) { |
024 |
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true); |
025 |
if(!$this->link_id){ |
026 |
$this->halt("数据库连接失败"); |
027 |
} |
028 |
} else { |
029 |
$this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw); |
030 |
if(!$this->link_id){ |
031 |
$this->halt("数据库持久连接失败"); |
032 |
} |
033 |
} |
034 |
if(!@mysql_select_db($dbname,$this->link_id)) { |
035 |
$this->halt('数据库选择失败'); |
036 |
} |
037 |
@mysql_query("set names ".$charset); |
038 |
} |
039 |
|
040 |
//查询 |
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); |
045 |
return $query; |
046 |
} |
047 |
|
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); |
053 |
return $rt; |
054 |
} |
055 |
|
056 |
//获取全部记录 |
057 |
public function get_all($sql,$result_type = MYSQL_ASSOC) { |
058 |
$query = $this->query($sql); |
059 |
$i = 0; |
060 |
$rt = array(); |
061 |
while($row =& mysql_fetch_array($query,$result_type)) { |
062 |
$rt[$i]=$row; |
063 |
$i++; |
064 |
} |
065 |
$this->write_log("获取全部记录 ".$sql); |
066 |
return $rt; |
067 |
} |
068 |
|
069 |
//插入 |
070 |
public function insert($table,$dataArray) { |
071 |
$field = ""; |
072 |
$value = ""; |
073 |
if( !is_array($dataArray) || count($dataArray)<=0) { |
074 |
$this->halt('没有要插入的数据'); |
075 |
return false; |
076 |
} |
077 |
while(list($key,$val)=each($dataArray)) { |
078 |
$field .="$key,"; |
079 |
$value .="'$val',"; |
080 |
} |
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; |
086 |
return true; |
087 |
} |
088 |
|
089 |
//更新 |
090 |
public function update( $table,$dataArray,$condition="") { |
091 |
if( !is_array($dataArray) || count($dataArray)<=0) { |
092 |
$this->halt('没有要更新的数据'); |
093 |
return false; |
094 |
} |
095 |
$value = ""; |
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; |
102 |
return true; |
103 |
} |
104 |
|
105 |
//删除 |
106 |
public function delete( $table,$condition="") { |
107 |
if( empty($condition) ) { |
108 |
$this->halt('没有设置删除的条件'); |
109 |
return false; |
110 |
} |
111 |
$sql = "delete from $table where 1=1 and $condition"; |
112 |
$this->write_log("删除 ".$sql); |
113 |
if(!$this->query($sql)) return false; |
114 |
return true; |
115 |
} |
116 |
|
117 |
//返回结果集 |
118 |
public function fetch_array($query, $result_type = MYSQL_ASSOC){ |
119 |
$this->write_log("返回结果集"); |
120 |
return mysql_fetch_array($query, $result_type); |
121 |
} |
122 |
|
123 |
//获取记录条数 |
124 |
public function num_rows($results) { |
125 |
if(!is_bool($results)) { |
126 |
$num = mysql_num_rows($results); |
127 |
$this->write_log("获取的记录条数为".$num); |
128 |
return $num; |
129 |
} else { |
130 |
return 0; |
131 |
} |
132 |
} |
133 |
|
134 |
//释放结果集 |
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); |
140 |
} |
141 |
} |
142 |
$this->write_log("释放结果集"); |
143 |
} |
144 |
|
145 |
//获取最后插入的id |
146 |
public function insert_id() { |
147 |
$id = mysql_insert_id($this->link_id); |
148 |
$this->write_log("最后插入的id为".$id); |
149 |
return $id; |
150 |
} |
151 |
|
152 |
//关闭数据库连接 |
153 |
protected function close() { |
154 |
$this->write_log("已关闭数据库连接"); |
155 |
return @mysql_close($this->link_id); |
156 |
} |
157 |
|
158 |
//错误提示 |
159 |
private function halt($msg='') { |
160 |
$msg .= "\r\n".mysql_error(); |
161 |
$this->write_log($msg); |
162 |
die($msg); |
163 |
} |
164 |
|
165 |
//析构函数 |
166 |
public function __destruct() { |
167 |
$this->free_result(); |
168 |
$use_time = ($this-> microtime_float())-($this->time); |
169 |
$this->write_log("完成整个查询任务,所用时间为".$use_time); |
170 |
if($this->is_log){ |
171 |
fclose($this->handle); |
172 |
} |
173 |
} |
174 |
|
175 |
//写入日志文件 |
176 |
public function write_log($msg=''){ |
177 |
if($this->is_log){ |
178 |
$text = date("Y-m-d H:i:s")." ".$msg."\r\n"; |
179 |
fwrite($this->handle,$text); |
180 |
} |
181 |
} |
182 |
|
183 |
//获取毫秒数 |
184 |
public function microtime_float() { |
185 |
list($usec, $sec) = explode(" ", microtime()); |
186 |
return ((float)$usec + (float)$sec); |
187 |
} |
188 |
} |
189 |
|
190 |
?> |
[代码] config.db.php
01 |
<?php |
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"] = './';//开启日志 |
10 |
?> |
浙公网安备 33010602011771号