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