1 <?php
2 Class DB {
3 private $link_id;
4 private $handle;
5 private $is_log;
6 private $time;
7 //构造函数
8 public function __construct() {
9 $this->time = $this->microtime_float();
10 require_once("config.db.php");
11 $this->connect($db_config["hostname"], $db_config["username"], $db_config["password"], $db_config["database"], $db_config["pconnect"]);
12 $this->is_log = $db_config["log"];
13 if($this->is_log){
14 $handle = fopen($db_config["logfilepath"]."dblog.txt", "a+");
15 $this->handle=$handle;
16 }
17 }
18
19 //数据库连接
20 public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset='utf8') {
21 if( $pconnect==0 ) {
22 $this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
23 if(!$this->link_id){
24 $this->halt("数据库连接失败");
25 }
26 } else {
27 $this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw);
28 if(!$this->link_id){
29 $this->halt("数据库持久连接失败");
30 }
31 }
32 if(!@mysql_select_db($dbname,$this->link_id)) {
33 $this->halt('数据库选择失败');
34 }
35 @mysql_query("set names ".$charset);
36 }
37
38 //查询
39 public function query($sql) {
40 $this->write_log("查询 ".$sql);
41 $query = mysql_query($sql,$this->link_id);
42 if(!$query) $this->halt('Query Error: ' . $sql);
43 return $query;
44 }
45
46 //获取一条记录(MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH)
47 public function get_one($sql,$result_type = MYSQL_ASSOC) {
48 $query = $this->query($sql);
49 $rt =& mysql_fetch_array($query,$result_type);
50 $this->write_log("获取一条记录 ".$sql);
51 return $rt;
52 }
53 //获取全部记录
54 public function get_all($sql,$result_type = MYSQL_ASSOC) {
55 $query = $this->query($sql);
56 $i = 0;
57 $rt = array();
58 while($row =& mysql_fetch_array($query,$result_type)) {
59 $rt[$i]=$row;
60 $i++;
61 }
62 $this->write_log("获取全部记录 ".$sql);
63 return $rt;
64 }
65
66 //插入
67 public function insert($table,$dataArray) {
68 $field = "";
69 $value = "";
70 if( !is_array($dataArray) || count($dataArray)<=0) {
71 $this->halt('没有要插入的数据');
72 return false;
73 }
74 while(list($key,$val)=each($dataArray)) {
75 $field .="$key,";
76 $value .="'$val',";
77 }
78 $field = substr( $field,0,-1);
79 $value = substr( $value,0,-1);
80 $sql = "insert into $table($field) values($value)";
81 $this->write_log("插入 ".$sql);
82 if(!$this->query($sql)) return false;
83 return true;
84 }
85 //更新
86 public function update( $table,$dataArray,$condition="") {
87 if( !is_array($dataArray) || count($dataArray)<=0) {
88 $this->halt('没有要更新的数据');
89 return false;
90 }
91 $value = "";
92 while( list($key,$val) = each($dataArray))
93 $value .= "$key = '$val',";
94 $value .= substr( $value,0,-1);
95 $sql = "update $table set $value where 1=1 and $condition";
96 $this->write_log("更新 ".$sql);
97 if(!$this->query($sql)) return false;
98 return true;
99 }
100 //删除
101 public function delete( $table,$condition="") {
102 if( empty($condition) ) {
103 $this->halt('没有设置删除的条件');
104 return false;
105 }
106 $sql = "delete from $table where 1=1 and $condition";
107 $this->write_log("删除 ".$sql);
108 if(!$this->query($sql)) return false;
109 return true;
110 }
111 //返回结果集
112 public function fetch_array($query, $result_type = MYSQL_ASSOC){
113 $this->write_log("返回结果集");
114 return mysql_fetch_array($query, $result_type);
115 }
116 //获取记录条数
117 public function num_rows($results) {
118 if(!is_bool($results)) {
119 $num = mysql_num_rows($results);
120 $this->write_log("获取的记录条数为".$num);
121 return $num;
122 } else {
123 return 0;
124 }
125 }
126 //释放结果集
127 public function free_result() {
128 $void = func_get_args();
129 foreach($void as $query) {
130 if(is_resource($query) && get_resource_type($query) === 'mysql result') {
131 return mysql_free_result($query);
132 }
133 }
134 $this->write_log("释放结果集");
135 }
136 //获取最后插入的id
137 public function insert_id() {
138 $id = mysql_insert_id($this->link_id);
139 $this->write_log("最后插入的id为".$id);
140 return $id;
141 }
142 //关闭数据库连接
143 protected function close() {
144 $this->write_log("已关闭数据库连接");
145 return @mysql_close($this->link_id);
146 }
147 //错误提示
148 private function halt($msg='') {
149 $msg .= "\r\n".mysql_error();
150 $this->write_log($msg);
151 die($msg);
152 }
153 //析构函数
154 public function __destruct() {
155 $this->free_result();
156 $use_time = ($this-> microtime_float())-($this->time);
157 $this->write_log("完成整个查询任务,所用时间为".$use_time);
158 if($this->is_log){
159 fclose($this->handle);
160 }
161 }
162
163 //写入日志文件
164 public function write_log($msg=''){
165 if($this->is_log){
166 $text = date("Y-m-d H:i:s")." ".$msg."\r\n";
167 fwrite($this->handle,$text);
168 }
169 }
170
171 //获取毫秒数
172 public function microtime_float() {
173 list($usec, $sec) = explode(" ", microtime());
174 return ((float)$usec + (float)$sec);
175 }
176 }
177 ?>