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