1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MySQL Related Functionality |
4
|
|
|
* @author Joe Huss <[email protected]> |
5
|
|
|
* @copyright 2019 |
6
|
|
|
* @package MyAdmin |
7
|
|
|
* @category SQL |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MyDb\Mysqli; |
11
|
|
|
|
12
|
|
|
use \MyDb\Generic; |
13
|
|
|
use \MyDb\Db_Interface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Db |
17
|
|
|
* |
18
|
|
|
* @access public |
19
|
|
|
*/ |
20
|
|
|
class Db extends Generic implements Db_Interface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $type = 'mysqli'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* alias function of select_db, changes the database we are working with. |
29
|
|
|
* |
30
|
|
|
* @param string $database the name of the database to use |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
1 |
|
public function useDb($database) |
34
|
|
|
{ |
35
|
1 |
|
$this->selectDb($database); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* changes the database we are working with. |
40
|
|
|
* |
41
|
|
|
* @param string $database the name of the database to use |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
1 |
|
public function selectDb($database) |
45
|
|
|
{ |
46
|
1 |
|
$this->connect(); |
47
|
1 |
|
mysqli_select_db($this->linkId, $database); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* public: connection management */ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Db::connect() |
54
|
|
|
* @param string $database |
55
|
|
|
* @param string $host |
56
|
|
|
* @param string $user |
57
|
|
|
* @param string $password |
58
|
|
|
* @return int|\mysqli |
59
|
|
|
*/ |
60
|
26 |
|
public function connect($database = '', $host = '', $user = '', $password = '') |
61
|
|
|
{ |
62
|
|
|
/* Handle defaults */ |
63
|
26 |
|
if ($database == '') { |
64
|
26 |
|
$database = $this->database; |
65
|
|
|
} |
66
|
26 |
|
if ($host == '') { |
67
|
26 |
|
$host = $this->host; |
68
|
|
|
} |
69
|
26 |
|
if ($user == '') { |
70
|
26 |
|
$user = $this->user; |
71
|
|
|
} |
72
|
26 |
|
if ($password == '') { |
73
|
26 |
|
$password = $this->password; |
74
|
|
|
} |
75
|
|
|
/* establish connection, select database */ |
76
|
26 |
|
if (!is_object($this->linkId)) { |
77
|
26 |
|
$this->connectionAttempt++; |
78
|
26 |
|
if ($this->connectionAttempt > 1) { |
79
|
6 |
|
error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
80
|
|
|
} |
81
|
26 |
|
if ($this->connectionAttempt >= $this->maxConnectErrors) { |
82
|
|
|
$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error); |
|
|
|
|
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
26 |
|
$this->linkId = mysqli_init(); |
86
|
26 |
|
$this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
87
|
26 |
|
$this->linkId->real_connect($host, $user, $password, $database); |
88
|
26 |
|
$this->linkId->set_charset($this->characterSet); |
89
|
26 |
|
if ($this->linkId->connect_errno) { |
90
|
|
|
$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error); |
91
|
|
|
return 0; |
92
|
|
|
} |
93
|
|
|
} |
94
|
26 |
|
return $this->linkId; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Db::disconnect() |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
1 |
|
public function disconnect() |
102
|
|
|
{ |
103
|
1 |
|
$return = method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
104
|
1 |
|
$this->linkId = 0; |
105
|
1 |
|
return $return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $string |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
2 |
|
public function real_escape($string = '') |
113
|
|
|
{ |
114
|
2 |
|
if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
|
|
|
115
|
|
|
return $this->escape($string); |
116
|
|
|
} |
117
|
2 |
|
return mysqli_real_escape_string($this->linkId, $string); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* discard the query result |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
1 |
|
public function free() |
125
|
|
|
{ |
126
|
1 |
|
if (is_resource($this->queryId)) { |
|
|
|
|
127
|
|
|
@mysqli_free_result($this->queryId); |
|
|
|
|
128
|
|
|
} |
129
|
1 |
|
$this->queryId = 0; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Db::queryReturn() |
134
|
|
|
* |
135
|
|
|
* Sends an SQL query to the server like the normal query() command but iterates through |
136
|
|
|
* any rows and returns the row or rows immediately or FALSE on error |
137
|
|
|
* |
138
|
|
|
* @param mixed $query SQL Query to be used |
139
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
140
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
141
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
142
|
|
|
*/ |
143
|
1 |
|
public function queryReturn($query, $line = '', $file = '') |
144
|
|
|
{ |
145
|
1 |
|
$this->query($query, $line, $file); |
146
|
1 |
|
if ($this->num_rows() == 0) { |
147
|
1 |
|
return false; |
148
|
1 |
|
} elseif ($this->num_rows() == 1) { |
149
|
1 |
|
$this->next_record(MYSQLI_ASSOC); |
150
|
1 |
|
return $this->Record; |
151
|
|
|
} else { |
152
|
1 |
|
$out = []; |
153
|
1 |
|
while ($this->next_record(MYSQLI_ASSOC)) { |
154
|
1 |
|
$out[] = $this->Record; |
155
|
|
|
} |
156
|
1 |
|
return $out; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* db:qr() |
162
|
|
|
* |
163
|
|
|
* alias of queryReturn() |
164
|
|
|
* |
165
|
|
|
* @param mixed $query SQL Query to be used |
166
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
167
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
168
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
169
|
|
|
*/ |
170
|
1 |
|
public function qr($query, $line = '', $file = '') |
171
|
|
|
{ |
172
|
1 |
|
return $this->queryReturn($query, $line, $file); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* creates a prepaired statement from query |
177
|
|
|
* |
178
|
|
|
* @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
179
|
|
|
* @return int|\MyDb\Mysqli\mysqli_stmt |
|
|
|
|
180
|
|
|
* @param string $line |
181
|
|
|
* @param string $file |
182
|
|
|
*/ |
183
|
1 |
|
public function prepare($query, $line = '', $file = '') |
184
|
|
|
{ |
185
|
1 |
|
if (!$this->connect()) { |
186
|
|
|
return 0; |
187
|
|
|
} |
188
|
1 |
|
$haltPrev = $this->haltOnError; |
|
|
|
|
189
|
1 |
|
$this->haltOnError = 'no'; |
190
|
1 |
|
$start = microtime(true); |
191
|
1 |
|
$prepare = mysqli_prepare($this->linkId, $query); |
|
|
|
|
192
|
1 |
|
$this->addLog($query, microtime(true) - $start, $line, $file); |
193
|
1 |
|
return $prepare; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Db::query() |
198
|
|
|
* |
199
|
|
|
* Sends an SQL query to the database |
200
|
|
|
* |
201
|
|
|
* @param mixed $queryString |
202
|
|
|
* @param string $line |
203
|
|
|
* @param string $file |
204
|
|
|
* @return mixed 0 if no query or query id handler, safe to ignore this return |
205
|
|
|
*/ |
206
|
10 |
|
public function query($queryString, $line = '', $file = '') |
207
|
|
|
{ |
208
|
|
|
/* No empty queries, please, since PHP4 chokes on them. */ |
209
|
|
|
/* The empty query string is passed on from the constructor, |
210
|
|
|
* when calling the class without a query, e.g. in situations |
211
|
|
|
* like these: '$db = new db_Subclass;' |
212
|
|
|
*/ |
213
|
10 |
|
if ($queryString == '') { |
214
|
1 |
|
return 0; |
215
|
|
|
} |
216
|
10 |
|
if (!$this->connect()) { |
217
|
|
|
return 0; |
218
|
|
|
/* we already complained in connect() about that. */ |
219
|
|
|
} |
220
|
10 |
|
$haltPrev = $this->haltOnError; |
221
|
10 |
|
$this->haltOnError = 'no'; |
222
|
|
|
// New query, discard previous result. |
223
|
10 |
|
if (is_resource($this->queryId)) { |
|
|
|
|
224
|
|
|
$this->free(); |
225
|
|
|
} |
226
|
10 |
|
if ($this->Debug) { |
227
|
1 |
|
printf("Debug: query = %s<br>\n", $queryString); |
228
|
|
|
} |
229
|
10 |
|
if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
230
|
|
|
$this->log($queryString, $line, $file); |
231
|
|
|
} |
232
|
10 |
|
$tries = 3; |
233
|
10 |
|
$try = 0; |
234
|
10 |
|
$this->queryId = false; |
235
|
10 |
|
while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
236
|
10 |
|
$try++; |
237
|
10 |
|
if ($try > 1) { |
238
|
|
|
@mysqli_close($this->linkId); |
|
|
|
|
239
|
|
|
$this->connect(); |
240
|
|
|
} |
241
|
10 |
|
$start = microtime(true); |
242
|
10 |
|
$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
|
|
|
243
|
10 |
|
$this->addLog($queryString, microtime(true) - $start, $line, $file); |
244
|
10 |
|
$this->Row = 0; |
245
|
10 |
|
$this->Errno = @mysqli_errno($this->linkId); |
|
|
|
|
246
|
10 |
|
$this->Error = @mysqli_error($this->linkId); |
|
|
|
|
247
|
10 |
|
if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
248
|
|
|
$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
249
|
|
|
} |
250
|
|
|
} |
251
|
10 |
|
$this->haltOnError = $haltPrev; |
252
|
10 |
|
if (null === $this->queryId || $this->queryId === false) { |
253
|
|
|
$this->halt('', $line, $file); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// Will return nada if it fails. That's fine. |
257
|
10 |
|
return $this->queryId; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return array|null|object |
262
|
|
|
*/ |
263
|
1 |
|
public function fetchObject() |
264
|
|
|
{ |
265
|
1 |
|
$this->Record = @mysqli_fetch_object($this->queryId); |
|
|
|
|
266
|
1 |
|
return $this->Record; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/* public: walk result set */ |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Db::next_record() |
273
|
|
|
* |
274
|
|
|
* @param mixed $resultType |
275
|
|
|
* @return bool |
276
|
|
|
*/ |
277
|
6 |
|
public function next_record($resultType = MYSQLI_BOTH) |
278
|
|
|
{ |
279
|
6 |
|
if ($this->queryId === false) { |
|
|
|
|
280
|
|
|
$this->haltmsg('next_record called with no query pending.'); |
281
|
|
|
return 0; |
282
|
|
|
} |
283
|
|
|
|
284
|
6 |
|
$this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
|
|
|
285
|
6 |
|
++$this->Row; |
286
|
6 |
|
$this->Errno = mysqli_errno($this->linkId); |
|
|
|
|
287
|
6 |
|
$this->Error = mysqli_error($this->linkId); |
|
|
|
|
288
|
|
|
|
289
|
6 |
|
$stat = is_array($this->Record); |
290
|
6 |
|
if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
|
|
|
291
|
|
|
$this->free(); |
292
|
|
|
} |
293
|
6 |
|
return $stat; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* switch to position in result set |
298
|
|
|
* |
299
|
|
|
* @param integer $pos the row numbe starting at 0 to switch to |
300
|
|
|
* @return bool whetherit was successfu or not. |
301
|
|
|
*/ |
302
|
1 |
|
public function seek($pos = 0) |
303
|
|
|
{ |
304
|
1 |
|
$status = @mysqli_data_seek($this->queryId, $pos); |
|
|
|
|
305
|
1 |
|
if ($status) { |
306
|
1 |
|
$this->Row = $pos; |
307
|
|
|
} else { |
308
|
1 |
|
$this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
309
|
|
|
/* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
310
|
1 |
|
$rows = $this->num_rows(); |
311
|
1 |
|
@mysqli_data_seek($this->queryId, $rows); |
|
|
|
|
312
|
1 |
|
$this->Row = $rows; |
313
|
1 |
|
return false; |
314
|
|
|
} |
315
|
1 |
|
return true; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Initiates a transaction |
320
|
|
|
* |
321
|
|
|
* @return bool |
322
|
|
|
*/ |
323
|
26 |
|
public function transactionBegin() |
324
|
|
|
{ |
325
|
26 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
326
|
|
|
return true; |
327
|
|
|
} |
328
|
26 |
|
if (!$this->connect()) { |
329
|
|
|
return 0; |
330
|
|
|
} |
331
|
26 |
|
return mysqli_begin_transaction($this->linkId); |
|
|
|
|
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Commits a transaction |
336
|
|
|
* |
337
|
|
|
* @return bool |
338
|
|
|
*/ |
339
|
1 |
|
public function transactionCommit() |
340
|
|
|
{ |
341
|
1 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
342
|
|
|
return true; |
343
|
|
|
} |
344
|
1 |
|
return mysqli_commit($this->linkId); |
|
|
|
|
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Rolls back a transaction |
349
|
|
|
* |
350
|
|
|
* @return bool |
351
|
|
|
*/ |
352
|
26 |
|
public function transactionAbort() |
353
|
|
|
{ |
354
|
26 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
355
|
|
|
return true; |
356
|
|
|
} |
357
|
26 |
|
return mysqli_rollback($this->linkId); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* This will get the last insert ID created on the current connection. Should only be called after an insert query is |
362
|
|
|
* run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
363
|
|
|
* unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
364
|
|
|
* |
365
|
|
|
* @param string $table |
366
|
|
|
* @param string $field |
367
|
|
|
* @return int|string |
368
|
|
|
*/ |
369
|
2 |
|
public function getLastInsertId($table, $field) |
370
|
|
|
{ |
371
|
2 |
|
if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
372
|
|
|
return -1; |
373
|
|
|
} |
374
|
|
|
|
375
|
2 |
|
return @mysqli_insert_id($this->linkId); |
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/* public: table locking */ |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Db::lock() |
382
|
|
|
* @param mixed $table |
383
|
|
|
* @param string $mode |
384
|
|
|
* @return bool|int|\mysqli_result |
385
|
|
|
*/ |
386
|
1 |
|
public function lock($table, $mode = 'write') |
387
|
|
|
{ |
388
|
1 |
|
$this->connect(); |
389
|
1 |
|
$query = 'lock tables '; |
390
|
1 |
|
if (is_array($table)) { |
391
|
1 |
|
foreach ($table as $key => $value) { |
392
|
1 |
|
if ($key == 'read' && $key != 0) { |
393
|
|
|
$query .= "$value read, "; |
394
|
|
|
} else { |
395
|
1 |
|
$query .= "$value $mode, "; |
396
|
|
|
} |
397
|
|
|
} |
398
|
1 |
|
$query = mb_substr($query, 0, -2); |
399
|
|
|
} else { |
400
|
1 |
|
$query .= "$table $mode"; |
401
|
|
|
} |
402
|
1 |
|
$res = @mysqli_query($this->linkId, $query); |
|
|
|
|
403
|
1 |
|
if (!$res) { |
404
|
|
|
$this->halt("lock($table, $mode) failed."); |
405
|
|
|
return 0; |
406
|
|
|
} |
407
|
1 |
|
return $res; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Db::unlock() |
412
|
|
|
* @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
413
|
|
|
* @return bool|int|\mysqli_result |
414
|
|
|
*/ |
415
|
2 |
|
public function unlock($haltOnError = true) |
416
|
|
|
{ |
417
|
2 |
|
$this->connect(); |
418
|
|
|
|
419
|
2 |
|
$res = @mysqli_query($this->linkId, 'unlock tables'); |
|
|
|
|
420
|
2 |
|
if ($haltOnError === true && !$res) { |
421
|
|
|
$this->halt('unlock() failed.'); |
422
|
|
|
return 0; |
423
|
|
|
} |
424
|
2 |
|
return $res; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/* public: evaluate the result (size, width) */ |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Db::affectedRows() |
431
|
|
|
* @return int |
432
|
|
|
*/ |
433
|
2 |
|
public function affectedRows() |
434
|
|
|
{ |
435
|
2 |
|
return @mysqli_affected_rows($this->linkId); |
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Db::num_rows() |
440
|
|
|
* @return int |
441
|
|
|
*/ |
442
|
6 |
|
public function num_rows() |
443
|
|
|
{ |
444
|
6 |
|
return @mysqli_num_rows($this->queryId); |
|
|
|
|
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Db::num_fields() |
449
|
|
|
* @return int |
450
|
|
|
*/ |
451
|
1 |
|
public function num_fields() |
452
|
|
|
{ |
453
|
1 |
|
return @mysqli_num_fields($this->queryId); |
|
|
|
|
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* gets an array of the table names in teh current datase |
458
|
|
|
* |
459
|
|
|
* @return array |
460
|
|
|
*/ |
461
|
1 |
|
public function tableNames() |
462
|
|
|
{ |
463
|
1 |
|
$return = []; |
464
|
1 |
|
$this->query('SHOW TABLES'); |
465
|
1 |
|
$i = 0; |
466
|
1 |
|
while ($info = $this->queryId->fetch_row()) { |
|
|
|
|
467
|
1 |
|
$return[$i]['table_name'] = $info[0]; |
468
|
1 |
|
$return[$i]['tablespace_name'] = $this->database; |
469
|
1 |
|
$return[$i]['database'] = $this->database; |
470
|
1 |
|
++$i; |
471
|
|
|
} |
472
|
1 |
|
return $return; |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param $result |
478
|
|
|
* @param $row |
479
|
|
|
* @param int|string $field |
480
|
|
|
* @return bool |
481
|
|
|
*/ |
482
|
|
|
/* |
|
|
|
|
483
|
|
|
function mysqli_result($result, $row, $field = 0) { |
484
|
|
|
if ($result === false) return false; |
485
|
|
|
if ($row >= mysqli_num_rows($result)) return false; |
486
|
|
|
if (is_string($field) && !(mb_strpos($field, '.') === false)) { |
487
|
|
|
$tField = explode('.', $field); |
488
|
|
|
$field = -1; |
489
|
|
|
$tFields = mysqli_fetch_fields($result); |
490
|
|
|
for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) { |
491
|
|
|
if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) { |
492
|
|
|
$field = $id; |
493
|
|
|
break; |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
if ($field == -1) return false; |
497
|
|
|
} |
498
|
|
|
mysqli_data_seek($result, $row); |
499
|
|
|
$line = mysqli_fetch_array($result); |
500
|
|
|
return isset($line[$field]) ? $line[$field] : false; |
501
|
|
|
} |
502
|
|
|
*/ |
503
|
|
|
|