DbMysqli.class.php
7.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
class Util_Db_DbMysqli extends Util_Db_BaseDB
{
/**
* 数据连接对象
* @var object
*/
private $mMysqli;
/**
* SQL语句
* @var object
*/
private $mSql;
/**
* 回滚次数
* @var int
*/
private $mTransTimes = 0;
/**
* 结果集
* @var object
*/
private $mRs;
/**
* 模式
*/
private $mFetchMode = MYSQLI_ASSOC; //取得模式
public function __construct()
{
parent::__construct();
}
/**
* 执行多条语句或存储过程
*
* @param string $sql
* @param $fetchMode
* @return array
*/
public function multiQuery($sql, $fetchMode = MYSQLI_NUM)
{
$this->autoChoiseHost($sql);
if ($this->mInstance->multi_query($sql))
{
$allRows = array();
do
{
if($result = $this->mInstance->store_result())
{
while ($rows = $result->fetch_array($fetchMode))
{
$allRows[] = $rows;
}
$result->close();
}
if(!$this->mInstance->more_results())
{
break;
}
}while ($this->mInstance->next_result());
return $allRows;
}
else
{
//echo "(sql or produre) error:".$sql;
}
}
/**
* 预处理更新
*
* @param string $sql SQL语句
* @param string $types 字段类型
* @param $fieldParams 字段
* @return int
*/
public function stmtUpdate($sql, $types, $fieldParams)
{
$result = false;
$this->autoChoiseHost($sql);
$stmt = $this->mInstance->prepare($sql);
if(strlen($types) == count($fieldParams) && is_object($stmt))
{
$params = array(
'stmtUpdatestmt' => $stmt,
'stmtUpdatetypes' => $types
);
$params = array_merge($params, $fieldParams);
call_user_func_array('mysqli_stmt_bind_param', $this->_refValues($params));
$stmt->execute();
$result = $stmt->affected_rows;
$stmt->close();
}
return $result;
}
private function _refValues($arr)
{
if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
{
$refs = array();
foreach($arr as $key => $value)
{
$refs[$key] = &$arr[$key];
}
return $refs;
}
return $arr;
}
/**
* 得到一条记录
*
* @param string $sql
* @return array
*/
public function getOne($sql)
{
$this->query($sql, 1);
$this->mFetchMode = MYSQLI_NUM;
$row = $this->fetch();
$this->free();
return $row;
}
/**
* 获取多条记录
*
* @param string $sql
* @param string $limit
* @param $fetchMode
* @return array
*/
public function getMore($sql, $limit = null, $fetchMode = MYSQLI_NUM)
{
$this->query($sql, $limit);
$allRows = array();
$rows = array();
$this->mFetchMode = $fetchMode;
while($rows = $this->fetch())
{
$allRows[] = $rows;
}
$this->free();
return $allRows;
}
/**
* 获取所有记录
*
* @param string $sql
* @param string $limit
* @param $fetchMode
* @return array
*/
public function getAll($sql, $limit = null, $fetchMode = MYSQLI_NUM)
{
$this->query($sql, $limit);
$allRows = array();
$this->mFetchMode = $fetchMode;
while($rows = $this->fetch())
{
$allRows[] = $rows;
}
$this->free();
return $allRows;
}
/**
* 获取插入的ID
*
* @return int
*/
public function insert_id()
{
return $this->mInstance->insert_id;
}
/**
* 更新操作
*
* @param string $sql
* @return int
*/
public function update($sql)
{
$this->query($sql);
return $this->mInstance->affected_rows;
}
/**
* 获取数据连接对象
*
* @return object
*/
public function getMysqli()
{
return $this->mMysqli;
}
/**
* 启动事务
*
* @return void
*/
public function startTrans()
{
//数据rollback 支持
if ($this->mTransTimes == 0)
{
//设置为主库
$this->setDefaultConnect(true);
$this->setHostSwitch(true);
$this->mInstance->autocommit(false);
}
$this->mTransTimes++;
return;
}
/**
* 用于非自动提交状态下面的查询提交
*
* @return boolean
*/
public function commit()
{
if ($this->mTransTimes > 0)
{
$result = $this->mInstance->commit();
$this->mInstance->autocommit(true);
$this->setHostSwitch(false);
$this->mTransTimes = 0;
if(!$result)
{
return false;
}
}
return true;
}
/**
* 事务回滚
*
* @return boolean
*/
public function rollback()
{
if ($this->mTransTimes > 0)
{
$result = $this->mInstance->rollback();
$this->setHostSwitch(false);
$this->mTransTimes = 0;
if(!$result)
{
return false;
}
}
return true;
}
/**
* 获取错误
*
* @return string
*/
public function error()
{
return $this->mInstance->error;
}
/**
* 查询语句
*
* @param string $sql
* @param string $limit
* @return object
*/
private function query($sql, $limit = null)
{
$this->autoChoiseHost($sql);
$sql = $this->getQuerySql($sql, $limit);
$this->mSql = $sql;
$this->mRs = $this->mInstance->query($sql);
if (!$this->mRs)
{
echo "<h2>".$this->mInstance->error."</h2>";
die();
}
else
{
return $this->mRs;
}
}
/**
* 获取结果集
*
* @return array
*/
private function fetch()
{
return @$this->mRs->fetch_array($this->mFetchMode);
}
/**
* 获取带limit的sql
*
* @param strinng $sql
* @param string $limit
* @return string
*/
private function getQuerySql($sql, $limit = null)
{
if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql))
{
$sql .= " LIMIT " . $limit;
}
return $sql;
}
/**
* 释放结果集
*/
private function free()
{
@$this->mRs->free();
}
/**
* 关闭数据库
*/
private function close()
{
if(is_resource($this->mInstance))
{
$this->mInstance->close();
}
}
/**
* 析构
*/
public function __destruct()
{
if(is_object($this->mRs))
{
$this->free();
}
$this->close();
unset($this->mInstance);
parent::__destruct();
}
}
?>