-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.class.php
More file actions
329 lines (292 loc) · 8.71 KB
/
Copy pathdb.class.php
File metadata and controls
329 lines (292 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* 操作数据库mysql 的类,完成增删改查的操作
*
*/
class ms_new_mysql {
private $dbHost;
private $dbUser;
private $dbPassword;
private $dbTable;
private $dbConn;
private $result;
private $sql;
private $pre;
private $coding;
private $pcon;
private $queryNum;
private static $daoImpl;
public function __construct($dbHost, $dbUser, $dbPassword, $dbTable, $pre, $coding, $pcon) {
$this->dbHost = $dbHost;
$this->dbUser = $dbUser;
$this->dbPassword = $dbPassword;
$this->dbTable = $dbTable;
$this->pre = $pre;
$this->coding = $coding;
$this->pcon = $pcon;
$this->connect();
$this->select_db($dbTable);
}
public function __clone() {
return self::$daoImpl;
}
public static function getDaoImpl($linkConfig) {
if (empty(self::$daoImpl)) {
self::$daoImpl = new ms_new_mysql($linkConfig['db']['dbhost'], $linkConfig['db']['dbuser'], $linkConfig['db']['dbpw'], $linkConfig['db']['dbname'], $linkConfig['db']['tablepre'], $linkConfig['db']['dbcharset'], $linkConfig['db']['pconnect']);
}
return self::$daoImpl;
}
public function connect() {
$func = $this->pcon == 1 ? "mysql_pconnect" : "mysql_connect";
//$this->dbConn = @$func($this->dbHost, $this->dbUser, $this->dbPassword);
$this->dbConn = mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->pcon);
if (!$this->dbConn) {
$this->halt("不能链接数据库", $this->sql);
return false;
}
if ($this->version() > '4.1') {
$serverset = $this->coding ? "character_set_connection='$this->coding',character_set_results='$this->coding',character_set_client=binary" : '';
$serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',') . " sql_mode='' ") : '';
$serverset && mysql_query("SET $serverset", $this->dbConn);
}
return $this->dbConn;
}
/**
* 选择一个数据库
*
* @param string $dbTable
* @return boolean
*/
public function select_db($dbTable) {
if (!@mysql_select_db($dbTable, $this->dbConn)) {
$this->halt("没有" . $dbTable . "这个数据库");
return false;
} else {
$this->dbTable = $dbTable;
return true;
}
}
/**
* 查询语句,传过来一个sql,执行,如果语句正确,返回结果集资源
*
* @param string $sql
* @param string $type
* @return Resouce query
*/
public function query($sql, $type = '') {
if ($query = mysql_query($sql, $this->dbConn)) {
$this->queryNum++;
return $query;
} else {
$this->halt("Mysql 查询出错", $sql);
return false;
}
}
/**
* 插入一组数据或一条数据
*
* @param string $tableName
* @param boolean
*/
public function insert($tableName, $info) {
$this->checkFields($tableName, $info);
$insert_sql = "INSERT INTO `$tableName`(`" . implode('`,`', array_keys($info)) . "`) VALUES('" . implode("','", $info) . "')";
return $this->query("$insert_sql");
}
/**
* 更细数据库中的数据
*
* @param string $tableName
* @param array $info
* @param string $where
* @return boolean
*/
public function update($tableName, $info, $where = '') {
$this->checkFields($tableName, $info);
if ($where) {
$sql = '';
foreach ($info as $k => $v) {
$sql .= ", `$k`='$v'";
}
$sql = substr($sql, 1);
$sql = "UPDATE `$tableName` SET $sql WHERE $where";
} else {
$sql = "REPLACE INTO `$tableName`(`" . implode('`,`', array_keys($info)) . "`) VALUES('" . implode("','", $info) . "')";
}
return $this->query($sql);
}
/**
* 检查一个字段是否在这张表中存在
*
* @param string $tableName
* @param array $array
* @return message
*/
public function checkFields($tableName, $array) {
$fields = $this->getFields($tableName);
foreach ($array as $key => $val) {
if (!in_array($key, $fields)) {
$this->halt("Mysql 错误", "找不到" . $key . "这个字段在" . $tableName . "里面");
return false;
}
}
}
/**
* 获取一张表中的所有字段
*
* @param string $tableName
* @return array fileds
*/
public function getFields($tableName) {
$fields = array();
$result = $this->query("SHOW COLUMNS FROM `$tableName`");
while ($list = $this->fetchArray($result)) {
$fields[] = $list['Field'];
}
$this->freeResult($result);
return $fields;
}
/**
* 释放当前数据库结果集的内存
*
* @param Resource $result
* @return null
*/
public function freeResult($result) {
return @mysql_free_result($result);
}
/**
* 使用while 可以迭代输出 一个结果集中的所有数据
*
* @param Resouce $query
* @param result type $result_type
* @return array
*/
public function fetchArray($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
/**
* 返回一个结果集中的一条数据
*
* @param $sql $query
* @param string $type
*
*
*/
public function getOne($sql, $type = '', $expires = 3600) {
$query = $this->query($sql, $type, $expires);
$rs = $this->fetchArray($query);
$this->freeResult($rs);
return $rs;
}
/**
* 获取插入的数据的返回的insetid
*
* @return insetid
*/
public function insertId() {
return mysql_insert_id($this->dbConn);
}
/**
* 获取当前的结果集中存在多少条数据
*
* @param string $query
* @return int nums
*/
public function numRows($query) {
return @mysql_numrows($query);
}
/**
* 获取当前的结果集中,有多少个字段
*
* @param Resouce $query
* @return int fields nums
*/
public function numFields($query) {
return @mysql_num_fields($query);
}
/**
* 获取当前执行的sql总条数
*
* @return queryNum
*/
public function getQueryNum() {
return $this->queryNum;
}
/**
* 获取当前文件中的函数,传入一个当前类存在函数,单例调用
*
* @param unknown_type $funcname
* @param unknown_type $params
* @return unknown
*/
public function getFunc($funcname, $params = '') {
if (empty($params)) {
return $this->$funcname();
} else {
return $this->$funcname($this->getFuncParams($params));
}
}
/**
* 如果是一个数组,那么拼接起来,处理返回一个参数集合
*
* @param array,string $params
* @return string a,d,3
*/
public function getFuncParams($params) {
$returnStr = "";
if (is_array($params)) {
foreach ($params as $key => $val) {
$returnStr .= $val . ",";
}
return rtrim($returnStr, ",");
} else {
return $params;
}
}
/**
* 获取当前数据库的版本信息
*
* @return version
*/
public function version() {
return mysql_get_server_info($this->dbConn);
}
/**
* 获取当前mysql数据的报错号
*
* @return errorno
*/
public function errno() {
return intval(@mysql_errno($this->dbConn));
}
/**
* 获取当前数据库的 提示信息
*
* @return unknown
*/
public function error() {
return @mysql_error($this->dbConn);
}
/**
* 操作数据库出错,提示信息
*
* @param unknown_type $message
* @param unknown_type $sql
*/
function halt($message = '', $sql = '') {
$this->errormsg = "<b>MySQL Query : </b>$sql <br /><b> MySQL Error : </b>" . $this->error() . " <br /> <b>MySQL Errno : </b>" . $this->errno() . " <br /><b> Message : </b> $message";
exit($this->errormsg);
}
function showTable() {
$tables = array();
$result = $this->query("SHOW TABLES");
while ($list = $this->fetchArray($result)) {
$tables[] = $list['Tables_in_' . $this->dbTable];
}
$this->freeResult($result);
return $tables;
}
}
$dbObj = new ms_new_mysql("127.0.0.1", "root", "", "mukewang", "", "utf-8", "0");
?>