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
|
|
|
*/ |
181
|
1 |
|
public function prepare($query) |
182
|
|
|
{ |
183
|
1 |
|
if (!$this->connect()) { |
184
|
|
|
return 0; |
185
|
|
|
} |
186
|
1 |
|
$haltPrev = $this->haltOnError; |
|
|
|
|
187
|
1 |
|
$this->haltOnError = 'no'; |
188
|
1 |
|
return mysqli_prepare($this->linkId, $query); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Db::query() |
193
|
|
|
* |
194
|
|
|
* Sends an SQL query to the database |
195
|
|
|
* |
196
|
|
|
* @param mixed $queryString |
197
|
|
|
* @param string $line |
198
|
|
|
* @param string $file |
199
|
|
|
* @return mixed 0 if no query or query id handler, safe to ignore this return |
200
|
|
|
*/ |
201
|
10 |
|
public function query($queryString, $line = '', $file = '') |
202
|
|
|
{ |
203
|
|
|
/* No empty queries, please, since PHP4 chokes on them. */ |
204
|
|
|
/* The empty query string is passed on from the constructor, |
205
|
|
|
* when calling the class without a query, e.g. in situations |
206
|
|
|
* like these: '$db = new db_Subclass;' |
207
|
|
|
*/ |
208
|
10 |
|
if ($queryString == '') { |
209
|
1 |
|
return 0; |
210
|
|
|
} |
211
|
10 |
|
if (!$this->connect()) { |
212
|
|
|
return 0; |
213
|
|
|
/* we already complained in connect() about that. */ |
214
|
|
|
} |
215
|
10 |
|
$haltPrev = $this->haltOnError; |
216
|
10 |
|
$this->haltOnError = 'no'; |
217
|
|
|
// New query, discard previous result. |
218
|
10 |
|
if (is_resource($this->queryId)) { |
|
|
|
|
219
|
|
|
$this->free(); |
220
|
|
|
} |
221
|
10 |
|
if ($this->Debug) { |
222
|
1 |
|
printf("Debug: query = %s<br>\n", $queryString); |
223
|
|
|
} |
224
|
10 |
|
if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
225
|
|
|
$this->log($queryString, $line, $file); |
226
|
|
|
} |
227
|
10 |
|
$tries = 3; |
228
|
10 |
|
$try = 0; |
229
|
10 |
|
$this->queryId = false; |
230
|
10 |
|
while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
231
|
10 |
|
$try++; |
232
|
10 |
|
if ($try > 1) { |
233
|
|
|
@mysqli_close($this->linkId); |
|
|
|
|
234
|
|
|
$this->connect(); |
235
|
|
|
} |
236
|
10 |
|
$start = microtime(true); |
237
|
10 |
|
$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
|
|
|
238
|
10 |
|
$this->addLog($queryString, microtime(true) - $start, $line, $file); |
239
|
10 |
|
$this->Row = 0; |
240
|
10 |
|
$this->Errno = @mysqli_errno($this->linkId); |
|
|
|
|
241
|
10 |
|
$this->Error = @mysqli_error($this->linkId); |
|
|
|
|
242
|
10 |
|
if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
243
|
|
|
$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
244
|
|
|
} |
245
|
|
|
} |
246
|
10 |
|
$this->haltOnError = $haltPrev; |
247
|
10 |
|
if (null === $this->queryId || $this->queryId === false) { |
248
|
|
|
$this->halt('', $line, $file); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// Will return nada if it fails. That's fine. |
252
|
10 |
|
return $this->queryId; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return array|null|object |
257
|
|
|
*/ |
258
|
1 |
|
public function fetchObject() |
259
|
|
|
{ |
260
|
1 |
|
$this->Record = @mysqli_fetch_object($this->queryId); |
|
|
|
|
261
|
1 |
|
return $this->Record; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/* public: walk result set */ |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Db::next_record() |
268
|
|
|
* |
269
|
|
|
* @param mixed $resultType |
270
|
|
|
* @return bool |
271
|
|
|
*/ |
272
|
6 |
|
public function next_record($resultType = MYSQLI_BOTH) |
273
|
|
|
{ |
274
|
6 |
|
if ($this->queryId === false) { |
|
|
|
|
275
|
|
|
$this->haltmsg('next_record called with no query pending.'); |
276
|
|
|
return 0; |
277
|
|
|
} |
278
|
|
|
|
279
|
6 |
|
$this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
|
|
|
280
|
6 |
|
++$this->Row; |
281
|
6 |
|
$this->Errno = mysqli_errno($this->linkId); |
|
|
|
|
282
|
6 |
|
$this->Error = mysqli_error($this->linkId); |
|
|
|
|
283
|
|
|
|
284
|
6 |
|
$stat = is_array($this->Record); |
285
|
6 |
|
if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
|
|
|
286
|
|
|
$this->free(); |
287
|
|
|
} |
288
|
6 |
|
return $stat; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* switch to position in result set |
293
|
|
|
* |
294
|
|
|
* @param integer $pos the row numbe starting at 0 to switch to |
295
|
|
|
* @return bool whetherit was successfu or not. |
296
|
|
|
*/ |
297
|
1 |
|
public function seek($pos = 0) |
298
|
|
|
{ |
299
|
1 |
|
$status = @mysqli_data_seek($this->queryId, $pos); |
|
|
|
|
300
|
1 |
|
if ($status) { |
301
|
1 |
|
$this->Row = $pos; |
302
|
|
|
} else { |
303
|
1 |
|
$this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
304
|
|
|
/* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
305
|
1 |
|
$rows = $this->num_rows(); |
306
|
1 |
|
@mysqli_data_seek($this->queryId, $rows); |
|
|
|
|
307
|
1 |
|
$this->Row = $rows; |
308
|
1 |
|
return false; |
309
|
|
|
} |
310
|
1 |
|
return true; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Initiates a transaction |
315
|
|
|
* |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
26 |
|
public function transactionBegin() |
319
|
|
|
{ |
320
|
26 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
321
|
|
|
return true; |
322
|
|
|
} |
323
|
26 |
|
if (!$this->connect()) { |
324
|
|
|
return 0; |
325
|
|
|
} |
326
|
26 |
|
return mysqli_begin_transaction($this->linkId); |
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Commits a transaction |
331
|
|
|
* |
332
|
|
|
* @return bool |
333
|
|
|
*/ |
334
|
1 |
|
public function transactionCommit() |
335
|
|
|
{ |
336
|
1 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
337
|
|
|
return true; |
338
|
|
|
} |
339
|
1 |
|
return mysqli_commit($this->linkId); |
|
|
|
|
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Rolls back a transaction |
344
|
|
|
* |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
26 |
|
public function transactionAbort() |
348
|
|
|
{ |
349
|
26 |
|
if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
350
|
|
|
return true; |
351
|
|
|
} |
352
|
26 |
|
return mysqli_rollback($this->linkId); |
|
|
|
|
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* This will get the last insert ID created on the current connection. Should only be called after an insert query is |
357
|
|
|
* run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
358
|
|
|
* unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
359
|
|
|
* |
360
|
|
|
* @param string $table |
361
|
|
|
* @param string $field |
362
|
|
|
* @return int|string |
363
|
|
|
*/ |
364
|
2 |
|
public function getLastInsertId($table, $field) |
365
|
|
|
{ |
366
|
2 |
|
if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
367
|
|
|
return -1; |
368
|
|
|
} |
369
|
|
|
|
370
|
2 |
|
return @mysqli_insert_id($this->linkId); |
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/* public: table locking */ |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Db::lock() |
377
|
|
|
* @param mixed $table |
378
|
|
|
* @param string $mode |
379
|
|
|
* @return bool|int|\mysqli_result |
380
|
|
|
*/ |
381
|
1 |
|
public function lock($table, $mode = 'write') |
382
|
|
|
{ |
383
|
1 |
|
$this->connect(); |
384
|
1 |
|
$query = 'lock tables '; |
385
|
1 |
|
if (is_array($table)) { |
386
|
1 |
|
foreach ($table as $key => $value) { |
387
|
1 |
|
if ($key == 'read' && $key != 0) { |
388
|
|
|
$query .= "$value read, "; |
389
|
|
|
} else { |
390
|
1 |
|
$query .= "$value $mode, "; |
391
|
|
|
} |
392
|
|
|
} |
393
|
1 |
|
$query = mb_substr($query, 0, -2); |
394
|
|
|
} else { |
395
|
1 |
|
$query .= "$table $mode"; |
396
|
|
|
} |
397
|
1 |
|
$res = @mysqli_query($this->linkId, $query); |
|
|
|
|
398
|
1 |
|
if (!$res) { |
399
|
|
|
$this->halt("lock($table, $mode) failed."); |
400
|
|
|
return 0; |
401
|
|
|
} |
402
|
1 |
|
return $res; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Db::unlock() |
407
|
|
|
* @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
408
|
|
|
* @return bool|int|\mysqli_result |
409
|
|
|
*/ |
410
|
2 |
|
public function unlock($haltOnError = true) |
411
|
|
|
{ |
412
|
2 |
|
$this->connect(); |
413
|
|
|
|
414
|
2 |
|
$res = @mysqli_query($this->linkId, 'unlock tables'); |
|
|
|
|
415
|
2 |
|
if ($haltOnError === true && !$res) { |
416
|
|
|
$this->halt('unlock() failed.'); |
417
|
|
|
return 0; |
418
|
|
|
} |
419
|
2 |
|
return $res; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/* public: evaluate the result (size, width) */ |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Db::affectedRows() |
426
|
|
|
* @return int |
427
|
|
|
*/ |
428
|
2 |
|
public function affectedRows() |
429
|
|
|
{ |
430
|
2 |
|
return @mysqli_affected_rows($this->linkId); |
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Db::num_rows() |
435
|
|
|
* @return int |
436
|
|
|
*/ |
437
|
6 |
|
public function num_rows() |
438
|
|
|
{ |
439
|
6 |
|
return @mysqli_num_rows($this->queryId); |
|
|
|
|
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Db::num_fields() |
444
|
|
|
* @return int |
445
|
|
|
*/ |
446
|
1 |
|
public function num_fields() |
447
|
|
|
{ |
448
|
1 |
|
return @mysqli_num_fields($this->queryId); |
|
|
|
|
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* gets an array of the table names in teh current datase |
453
|
|
|
* |
454
|
|
|
* @return array |
455
|
|
|
*/ |
456
|
1 |
|
public function tableNames() |
457
|
|
|
{ |
458
|
1 |
|
$return = []; |
459
|
1 |
|
$this->query('SHOW TABLES'); |
460
|
1 |
|
$i = 0; |
461
|
1 |
|
while ($info = $this->queryId->fetch_row()) { |
|
|
|
|
462
|
1 |
|
$return[$i]['table_name'] = $info[0]; |
463
|
1 |
|
$return[$i]['tablespace_name'] = $this->database; |
464
|
1 |
|
$return[$i]['database'] = $this->database; |
465
|
1 |
|
++$i; |
466
|
|
|
} |
467
|
1 |
|
return $return; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @param $result |
473
|
|
|
* @param $row |
474
|
|
|
* @param int|string $field |
475
|
|
|
* @return bool |
476
|
|
|
*/ |
477
|
|
|
/* |
|
|
|
|
478
|
|
|
function mysqli_result($result, $row, $field = 0) { |
479
|
|
|
if ($result === false) return false; |
480
|
|
|
if ($row >= mysqli_num_rows($result)) return false; |
481
|
|
|
if (is_string($field) && !(mb_strpos($field, '.') === false)) { |
482
|
|
|
$tField = explode('.', $field); |
483
|
|
|
$field = -1; |
484
|
|
|
$tFields = mysqli_fetch_fields($result); |
485
|
|
|
for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) { |
486
|
|
|
if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) { |
487
|
|
|
$field = $id; |
488
|
|
|
break; |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
if ($field == -1) return false; |
492
|
|
|
} |
493
|
|
|
mysqli_data_seek($result, $row); |
494
|
|
|
$line = mysqli_fetch_array($result); |
495
|
|
|
return isset($line[$field]) ? $line[$field] : false; |
496
|
|
|
} |
497
|
|
|
*/ |
498
|
|
|
|