Passed
Push — master ( 489bf9...2d77bd )
by Joe
04:47
created

Db::tableNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_select_db(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
		mysqli_select_db(/** @scrutinizer ignore-type */ $this->linkId, $database);
Loading history...
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 12
	public function connect($database = '', $host = '', $user = '', $password = '') {
58
		/* Handle defaults */
59 12
		if ($database == '')
60 12
			$database = $this->database;
61 12
		if ($host == '')
62 12
			$host = $this->host;
63 12
		if ($user == '')
64 12
			$user = $this->user;
65 12
		if ($password == '')
66 12
			$password = $this->password;
67
		/* establish connection, select database */
68 12
		if (!is_object($this->linkId)) {
0 ignored issues
show
introduced by
The condition is_object($this->linkId) is always false.
Loading history...
69 12
			$this->connectionAttempt++;
70 12
			if ($this->connectionAttempt > 1)
71 2
				error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}");
72 12
			if ($this->connectionAttempt >= $this->maxConnectErrors) {
73
				$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mysqli seems to be never defined.
Loading history...
74
				return 0;
75
			}
76 12
			$this->linkId = mysqli_init();
77 12
			$this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}");
78 12
			$this->linkId->real_connect($host, $user, $password, $database);
79 12
			$this->linkId->set_charset($this->characterSet);
80 12
			if ($this->linkId->connect_errno) {
81
				$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error);
82
				return 0;
83
			}
84
		}
85 12
		return $this->linkId;
86
	}
87
88
	/**
89
	 * Db::disconnect()
90
	 * @return bool
91
	 */
92
	public function disconnect() {
93
		$return = is_resource($this->linkId) ? $this->linkId->close() : false;
0 ignored issues
show
introduced by
The condition is_resource($this->linkId) is always false.
Loading history...
94
		$this->linkId = 0;
95
		return $return;
96
	}
97
98
	/**
99
	 * @param $string
100
	 * @return string
101
	 */
102 1
	public function real_escape($string) {
103 1
		if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect())
0 ignored issues
show
introduced by
The condition is_resource($this->linkId) is always false.
Loading history...
104
			return $this->escape($string);
105 1
		return mysqli_real_escape_string($this->linkId, $string);
106
	}
107
108
	/**
109
	 * @param $string
110
	 * @return string
111
	 */
112 1
	public function escape($string) {
113
		//if (function_exists('mysql_escape_string'))
114
			//return mysql_escape_string($string);
115 1
		return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $string);
116
	}
117
118
	/**
119
	 * Db::dbAddslashes()
120
	 * @param mixed $str
121
	 * @return string
122
	 */
123 1
	public function dbAddslashes($str) {
124 1
		if (!isset($str) || $str == '')
125
			return '';
126
127 1
		return addslashes($str);
128
	}
129
130
	/* public: discard the query result */
131
132
	/**
133
	 * Db::free()
134
	 * @return void
135
	 */
136
	public function free() {
137
		if (is_resource($this->queryId))
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
138
			@mysqli_free_result($this->queryId);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_free_result(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

138
			/** @scrutinizer ignore-unhandled */ @mysqli_free_result($this->queryId);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
139
		$this->queryId = 0;
140
	}
141
142
	/**
143
	 * Db::queryReturn()
144
	 *
145
	 * Sends an SQL query to the server like the normal query() command but iterates through
146
	 * any rows and returns the row or rows immediately or FALSE on error
147
	 *
148
	 * @param mixed $query SQL Query to be used
149
	 * @param string $line optionally pass __LINE__ calling the query for logging
150
	 * @param string $file optionally pass __FILE__ calling the query for logging
151
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
152
	 */
153 1
	public function queryReturn($query, $line = '', $file = '') {
154 1
		$this->query($query, $line, $file);
155 1
		if ($this->num_rows() == 0) {
156 1
			return false;
157 1
		} elseif ($this->num_rows() == 1) {
158 1
			$this->next_record(MYSQLI_ASSOC);
159 1
			return $this->Record;
160
		} else {
161 1
			$out = [];
162 1
			while ($this->next_record(MYSQLI_ASSOC))
163 1
				$out[] = $this->Record;
164 1
			return $out;
165
		}
166
	}
167
168
	/**
169
	 * db:qr()
170
	 *
171
	 *  alias of queryReturn()
172
	 *
173
	 * @param mixed $query SQL Query to be used
174
	 * @param string $line optionally pass __LINE__ calling the query for logging
175
	 * @param string $file optionally pass __FILE__ calling the query for logging
176
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
177
	 */
178 1
	public function qr($query, $line = '', $file = '') {
179 1
		return $this->queryReturn($query, $line, $file);
180
	}
181
182
	/**
183
	 * creates a prepaired statement from query
184
	 *
185
	 * @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 = ?
186
	 * @return int|\MyDb\Mysqli\mysqli_stmt
0 ignored issues
show
Bug introduced by
The type MyDb\Mysqli\mysqli_stmt was not found. Did you mean mysqli_stmt? If so, make sure to prefix the type with \.
Loading history...
187
	 */
188
	public function prepare($query) {
189
		if (!$this->connect())
190
			return 0;
191
		$haltPrev = $this->haltOnError;
0 ignored issues
show
Unused Code introduced by
The assignment to $haltPrev is dead and can be removed.
Loading history...
192
		$this->haltOnError = 'no';
193
		return mysqli_prepare($this->linkId, $query);
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 5
	public function query($queryString, $line = '', $file = '') {
207
		/* No empty queries, please, since PHP4 chokes on them. */
208
		/* The empty query string is passed on from the constructor,
209
		* when calling the class without a query, e.g. in situations
210
		* like these: '$db = new db_Subclass;'
211
		*/
212 5
		if ($queryString == '')
213
			return 0;
214 5
		if (!$this->connect()) {
215
			return 0;
216
			/* we already complained in connect() about that. */
217
		}
218 5
		$haltPrev = $this->haltOnError;
219 5
		$this->haltOnError = 'no';
220
		// New query, discard previous result.
221 5
		if (is_resource($this->queryId))
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
222
			$this->free();
223 5
		if ($this->Debug)
224
			printf("Debug: query = %s<br>\n", $queryString);
225 5
		if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false)
226
			$this->log($queryString, $line, $file);
227 5
		$tries = 3;
228 5
		$try = 0;
229 5
		$this->queryId = false;
230 5
		while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) {
231 5
			$try++;
232 5
			if ($try > 1) {
233
				@mysqli_close($this->linkId);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_close(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

233
				/** @scrutinizer ignore-unhandled */ @mysqli_close($this->linkId);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
234
				$this->connect();
235
			}
236 5
			$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT);
237 5
			$this->Row = 0;
238 5
			$this->Errno = @mysqli_errno($this->linkId);
239 5
			$this->Error = @mysqli_error($this->linkId);
240 5
			if ($try == 1 && (null === $this->queryId || $this->queryId === false)) {
241
				$email = "MySQLi Error<br>\n".'Query: '.$queryString."<br>\n".'Error #'.$this->Errno.': '.$this->Error."<br>\n".'Line: '.$line."<br>\n".'File: '.$file."<br>\n".(isset($GLOBALS['tf']) ? 'User: '.$GLOBALS['tf']->session->account_id."<br>\n" : '');
242
				$email .= '<br><br>Request Variables:<br>'.print_r($_REQUEST, true);
243
				$email .= '<br><br>Server Variables:<br>'.print_r($_SERVER, true);
244
				$subject = $_SERVER['HOSTNAME'].' MySQLi Error';
245
				$headers = '';
246
				$headers .= 'MIME-Version: 1.0'.PHP_EOL;
247
				$headers .= 'Content-type: text/html; charset=UTF-8'.PHP_EOL;
248
				$headers .= 'From: No-Reply <[email protected]>'.PHP_EOL;
249
				$headers .= 'X-Mailer: Trouble-Free.Net Admin Center'.PHP_EOL;
250
				mail('[email protected]', $subject, $email, $headers);
251
				mail('[email protected]', $subject, $email, $headers);
252
				$this->haltmsg('Invalid SQL: '.$queryString, $line, $file);
253
			}
254
		}
255 5
		$this->haltOnError = $haltPrev;
256 5
		if (null === $this->queryId || $this->queryId === false)
257
			$this->halt('', $line, $file);
258
259
		// Will return nada if it fails. That's fine.
260 5
		return $this->queryId;
261
	}
262
263
	/**
264
	 * @return array|null|object
265
	 */
266
	public function fetchObject() {
267
		$this->Record = @mysqli_fetch_object($this->queryId);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_fetch_object(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

267
		$this->Record = @mysqli_fetch_object(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
268
		return $this->Record;
269
	}
270
271
	/* public: walk result set */
272
273
	/**
274
	 * Db::next_record()
275
	 *
276
	 * @param mixed $resultType
277
	 * @return bool
278
	 */
279 4
	public function next_record($resultType = MYSQLI_BOTH) {
280 4
		if ($this->queryId === false) {
0 ignored issues
show
introduced by
The condition $this->queryId === false is always false.
Loading history...
281
			$this->halt('next_record called with no query pending.');
282
			return 0;
283
		}
284
285 4
		$this->Record = @mysqli_fetch_array($this->queryId, $resultType);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_fetch_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

285
		$this->Record = @mysqli_fetch_array(/** @scrutinizer ignore-type */ $this->queryId, $resultType);
Loading history...
286 4
		++$this->Row;
287 4
		$this->Errno = mysqli_errno($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_errno(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

287
		$this->Errno = mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
288 4
		$this->Error = mysqli_error($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_error(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
		$this->Error = mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
289
290 4
		$stat = is_array($this->Record);
291 4
		if (!$stat && $this->autoFree && is_resource($this->queryId))
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
292
			$this->free();
293 4
		return $stat;
294
	}
295
296
	/* public: position in result set */
297
298
	/**
299
	 * Db::seek()
300
	 * @param integer $pos
301
	 * @return int
302
	 */
303
	public function seek($pos = 0) {
304
		$status = @mysqli_data_seek($this->queryId, $pos);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_data_seek(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

304
		$status = @mysqli_data_seek(/** @scrutinizer ignore-type */ $this->queryId, $pos);
Loading history...
305
		if ($status) {
306
			$this->Row = $pos;
307
		} else {
308
			$this->halt("seek($pos) failed: result has ".$this->num_rows().' rows');
309
			/* half assed attempt to save the day,
310
			* but do not consider this documented or even
311
			* desirable behaviour.
312
			*/
313
			@mysqli_data_seek($this->queryId, $this->num_rows());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_data_seek(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

313
			/** @scrutinizer ignore-unhandled */ @mysqli_data_seek($this->queryId, $this->num_rows());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
314
			$this->Row = $this->num_rows;
0 ignored issues
show
Bug Best Practice introduced by
The property num_rows does not exist on MyDb\Mysqli\Db. Did you maybe forget to declare it?
Loading history...
315
			return 0;
316
		}
317
		return 1;
318
	}
319
320
	/**
321
	 * Initiates a transaction
322
	 *
323
	 * @return bool
324
	 */
325 12
	public function transactionBegin() {
326 12
		if (version_compare(PHP_VERSION, '5.5.0') < 0)
327
			return true;
328 12
		if (!$this->connect())
329
			return 0;
330 12
		return mysqli_begin_transaction($this->linkId);
331
	}
332
333
	/**
334
	 * Commits a transaction
335
	 *
336
	 * @return bool
337
	 */
338
	public function transactionCommit() {
339
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0)
340
			return true;
341
		return mysqli_commit($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_commit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

341
		return mysqli_commit(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
342
	}
343
344
	/**
345
	 * Rolls back a transaction
346
	 *
347
	 * @return bool
348
	 */
349 12
	public function transactionAbort() {
350 12
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0)
351
			return true;
352 12
		return mysqli_rollback($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_rollback(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

352
		return mysqli_rollback(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
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
	public function getLastInsertId($table, $field) {
365
		if (!isset($table) || $table == '' || !isset($field) || $field == '')
366
			return -1;
367
368
		return @mysqli_insert_id($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_insert_id(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

368
		return @mysqli_insert_id(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
369
	}
370
371
	/* public: table locking */
372
373
	/**
374
	 * Db::lock()
375
	 * @param mixed  $table
376
	 * @param string $mode
377
	 * @return bool|int|\mysqli_result
378
	 */
379
	public function lock($table, $mode = 'write') {
380
		$this->connect();
381
382
		$query = 'lock tables ';
383
		if (is_array($table)) {
384
			while (list($key, $value) = each($table)) {
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

384
			while (list($key, $value) = /** @scrutinizer ignore-deprecated */ each($table)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
385
				if ($key == 'read' && $key != 0) {
386
					$query .= "$value read, ";
387
				} else {
388
					$query .= "$value $mode, ";
389
				}
390
			}
391
			$query = mb_substr($query, 0, -2);
392
		} else {
393
			$query .= "$table $mode";
394
		}
395
		$res = @mysqli_query($this->linkId, $query);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_query(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

395
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
396
		if (!$res) {
397
			$this->halt("lock($table, $mode) failed.");
398
			return 0;
399
		}
400
		return $res;
401
	}
402
403
	/**
404
	 * Db::unlock()
405
	 * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error
406
	 * @return bool|int|\mysqli_result
407
	 */
408
	public function unlock($haltOnError = true) {
409
		$this->connect();
410
411
		$res = @mysqli_query($this->linkId, 'unlock tables');
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_query(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

411
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, 'unlock tables');
Loading history...
412
		if ($haltOnError === true && !$res) {
413
			$this->halt('unlock() failed.');
414
			return 0;
415
		}
416
		return $res;
417
	}
418
419
	/* public: evaluate the result (size, width) */
420
421
	/**
422
	 * Db::affectedRows()
423
	 * @return int
424
	 */
425
	public function affectedRows() {
426
		return @mysqli_affected_rows($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $link of mysqli_affected_rows(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

426
		return @mysqli_affected_rows(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
427
	}
428
429
	/**
430
	 * Db::num_rows()
431
	 * @return int
432
	 */
433 3
	public function num_rows() {
434 3
		return @mysqli_num_rows($this->queryId);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_num_rows(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

434
		return @mysqli_num_rows(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
435
	}
436
437
	/**
438
	 * Db::num_fields()
439
	 * @return int
440
	 */
441
	public function num_fields() {
442
		return @mysqli_num_fields($this->queryId);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_num_fields(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

442
		return @mysqli_num_fields(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
443
	}
444
445
	/**
446
	 * gets an array of the table names in teh current datase
447
	 *
448
	 * @return array
449
	 */
450 1
	public function tableNames() {
451 1
		$return = [];
452 1
		$this->query('SHOW TABLES');
453 1
		$i = 0;
454 1
		while ($info = $this->queryId->fetch_row()) {
0 ignored issues
show
Bug introduced by
The method fetch_row() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

454
		while ($info = $this->queryId->/** @scrutinizer ignore-call */ fetch_row()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
455 1
			$return[$i]['table_name'] = $info[0];
456 1
			$return[$i]['tablespace_name'] = $this->database;
457 1
			$return[$i]['database'] = $this->database;
458 1
			++$i;
459
		}
460 1
		return $return;
461
	}
462
463
	/**
464
	 * Db::createDatabase()
465
	 *
466
	 * @param string $adminname
467
	 * @param string $adminpasswd
468
	 * @return void
469
	 */
470
	public function createDatabase($adminname = '', $adminpasswd = '') {
471
		$currentUser = $this->user;
472
		$currentPassword = $this->password;
473
		$currentDatabase = $this->database;
474
475
		if ($adminname != '') {
476
			$this->user = $adminname;
477
			$this->password = $adminpasswd;
478
			$this->database = 'mysql';
479
		}
480
		$this->disconnect();
481
		$this->query("CREATE DATABASE $currentDatabase");
482
		$this->query("grant all on $currentDatabase.* to $currentUser@localhost identified by '{$currentPassword}'");
483
		$this->disconnect();
484
485
		$this->user = $currentUser;
486
		$this->password = $currentPassword;
487
		$this->database = $currentDatabase;
488
		$this->connect();
489
		/*return $return; */
490
	}
491
492
}
493
494
/**
495
 * @param $result
496
 * @param $row
497
 * @param int|string $field
498
 * @return bool
499
 */
500
function mysqli_result($result, $row, $field = 0) {
501
	if ($result === false) return false;
502
	if ($row >= mysqli_num_rows($result)) return false;
503
	if (is_string($field) && !(mb_strpos($field, '.') === false)) {
504
		$tField = explode('.', $field);
505
		$field = -1;
506
		$tFields = mysqli_fetch_fields($result);
507
		for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) {
508
			if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) {
509
				$field = $id;
510
				break;
511
			}
512
		}
513
		if ($field == -1) return false;
514
	}
515
	mysqli_data_seek($result, $row);
516
	$line = mysqli_fetch_array($result);
517
	return isset($line[$field]) ? $line[$field] : false;
518
}
519