Passed
Push — master ( 09eaa8...f9ddf1 )
by Joe
03:15
created

Db::dbAddslashes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 2
rs 10
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 13
	public function connect($database = '', $host = '', $user = '', $password = '') {
58
		/* Handle defaults */
59 13
		if ($database == '')
60 13
			$database = $this->database;
61 13
		if ($host == '')
62 13
			$host = $this->host;
63 13
		if ($user == '')
64 13
			$user = $this->user;
65 13
		if ($password == '')
66 13
			$password = $this->password;
67
		/* establish connection, select database */
68 13
		if (!is_object($this->linkId)) {
0 ignored issues
show
introduced by
The condition is_object($this->linkId) is always false.
Loading history...
69 13
			$this->connectionAttempt++;
70 13
			if ($this->connectionAttempt > 1)
71 3
				error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}");
72 13
			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 13
			$this->linkId = mysqli_init();
77 13
			$this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}");
78 13
			$this->linkId->real_connect($host, $user, $password, $database);
79 13
			$this->linkId->set_charset($this->characterSet);
80 13
			if ($this->linkId->connect_errno) {
81
				$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error);
82
				return 0;
83
			}
84
		}
85 13
		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 ($str == '')
125 1
			return '';
126 1
		return addslashes($str);
127
	}
128
129
	/* public: discard the query result */
130
131
	/**
132
	 * Db::free()
133
	 * @return void
134
	 */
135 1
	public function free() {
136 1
		if (is_resource($this->queryId))
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
137
			@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

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

232
				/** @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...
233
				$this->connect();
234
			}
235 5
			$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT);
236 5
			$this->Row = 0;
237 5
			$this->Errno = @mysqli_errno($this->linkId);
238 5
			$this->Error = @mysqli_error($this->linkId);
239 5
			if ($try == 1 && (null === $this->queryId || $this->queryId === false)) {
240
				$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" : '');
241
				$email .= '<br><br>Request Variables:<br>'.print_r($_REQUEST, true);
242
				$email .= '<br><br>Server Variables:<br>'.print_r($_SERVER, true);
243
				$subject = $_SERVER['HOSTNAME'].' MySQLi Error';
244
				$headers = '';
245
				$headers .= 'MIME-Version: 1.0'.PHP_EOL;
246
				$headers .= 'Content-type: text/html; charset=UTF-8'.PHP_EOL;
247
				$headers .= 'From: No-Reply <[email protected]>'.PHP_EOL;
248
				$headers .= 'X-Mailer: Trouble-Free.Net Admin Center'.PHP_EOL;
249
				mail('[email protected]', $subject, $email, $headers);
250
				mail('[email protected]', $subject, $email, $headers);
251
				$this->haltmsg('Invalid SQL: '.$queryString, $line, $file);
252
			}
253
		}
254 5
		$this->haltOnError = $haltPrev;
255 5
		if (null === $this->queryId || $this->queryId === false)
256
			$this->halt('', $line, $file);
257
258
		// Will return nada if it fails. That's fine.
259 5
		return $this->queryId;
260
	}
261
262
	/**
263
	 * @return array|null|object
264
	 */
265
	public function fetchObject() {
266
		$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

266
		$this->Record = @mysqli_fetch_object(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
267
		return $this->Record;
268
	}
269
270
	/* public: walk result set */
271
272
	/**
273
	 * Db::next_record()
274
	 *
275
	 * @param mixed $resultType
276
	 * @return bool
277
	 */
278 4
	public function next_record($resultType = MYSQLI_BOTH) {
279 4
		if ($this->queryId === false) {
0 ignored issues
show
introduced by
The condition $this->queryId === false is always false.
Loading history...
280
			$this->halt('next_record called with no query pending.');
281
			return 0;
282
		}
283
284 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

284
		$this->Record = @mysqli_fetch_array(/** @scrutinizer ignore-type */ $this->queryId, $resultType);
Loading history...
285 4
		++$this->Row;
286 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

286
		$this->Errno = mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
287 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

287
		$this->Error = mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
288
289 4
		$stat = is_array($this->Record);
290 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...
291
			$this->free();
292 4
		return $stat;
293
	}
294
295
	/* public: position in result set */
296
297
	/**
298
	 * Db::seek()
299
	 * @param integer $pos
300
	 * @return int
301
	 */
302
	public function seek($pos = 0) {
303
		$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

303
		$status = @mysqli_data_seek(/** @scrutinizer ignore-type */ $this->queryId, $pos);
Loading history...
304
		if ($status) {
305
			$this->Row = $pos;
306
		} else {
307
			$this->halt("seek($pos) failed: result has ".$this->num_rows().' rows');
308
			/* half assed attempt to save the day,
309
			* but do not consider this documented or even
310
			* desirable behaviour.
311
			*/
312
			@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

312
			/** @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...
313
			$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...
314
			return 0;
315
		}
316
		return 1;
317
	}
318
319
	/**
320
	 * Initiates a transaction
321
	 *
322
	 * @return bool
323
	 */
324 13
	public function transactionBegin() {
325 13
		if (version_compare(PHP_VERSION, '5.5.0') < 0)
326
			return true;
327 13
		if (!$this->connect())
328
			return 0;
329 13
		return mysqli_begin_transaction($this->linkId);
330
	}
331
332
	/**
333
	 * Commits a transaction
334
	 *
335
	 * @return bool
336
	 */
337
	public function transactionCommit() {
338
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0)
339
			return true;
340
		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

340
		return mysqli_commit(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
341
	}
342
343
	/**
344
	 * Rolls back a transaction
345
	 *
346
	 * @return bool
347
	 */
348 13
	public function transactionAbort() {
349 13
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0)
350
			return true;
351 13
		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

351
		return mysqli_rollback(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
352
	}
353
354
	/**
355
	 * This will get the last insert ID created on the current connection.  Should only be called after an insert query is
356
	 * run on a table that has an auto incrementing field.  $table and $field are required, but unused here since it's
357
	 * unnecessary for mysql.  For compatibility with pgsql, the params must be supplied.
358
	 *
359
	 * @param string $table
360
	 * @param string $field
361
	 * @return int|string
362
	 */
363
	public function getLastInsertId($table, $field) {
364
		if (!isset($table) || $table == '' || !isset($field) || $field == '')
365
			return -1;
366
367
		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

367
		return @mysqli_insert_id(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
368
	}
369
370
	/* public: table locking */
371
372
	/**
373
	 * Db::lock()
374
	 * @param mixed  $table
375
	 * @param string $mode
376
	 * @return bool|int|\mysqli_result
377
	 */
378
	public function lock($table, $mode = 'write') {
379
		$this->connect();
380
381
		$query = 'lock tables ';
382
		if (is_array($table)) {
383
			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

383
			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...
384
				if ($key == 'read' && $key != 0) {
385
					$query .= "$value read, ";
386
				} else {
387
					$query .= "$value $mode, ";
388
				}
389
			}
390
			$query = mb_substr($query, 0, -2);
391
		} else {
392
			$query .= "$table $mode";
393
		}
394
		$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

394
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
395
		if (!$res) {
396
			$this->halt("lock($table, $mode) failed.");
397
			return 0;
398
		}
399
		return $res;
400
	}
401
402
	/**
403
	 * Db::unlock()
404
	 * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error
405
	 * @return bool|int|\mysqli_result
406
	 */
407
	public function unlock($haltOnError = true) {
408
		$this->connect();
409
410
		$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

410
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, 'unlock tables');
Loading history...
411
		if ($haltOnError === true && !$res) {
412
			$this->halt('unlock() failed.');
413
			return 0;
414
		}
415
		return $res;
416
	}
417
418
	/* public: evaluate the result (size, width) */
419
420
	/**
421
	 * Db::affectedRows()
422
	 * @return int
423
	 */
424
	public function affectedRows() {
425
		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

425
		return @mysqli_affected_rows(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
426
	}
427
428
	/**
429
	 * Db::num_rows()
430
	 * @return int
431
	 */
432 3
	public function num_rows() {
433 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

433
		return @mysqli_num_rows(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
434
	}
435
436
	/**
437
	 * Db::num_fields()
438
	 * @return int
439
	 */
440
	public function num_fields() {
441
		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

441
		return @mysqli_num_fields(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
442
	}
443
444
	/**
445
	 * gets an array of the table names in teh current datase
446
	 *
447
	 * @return array
448
	 */
449 1
	public function tableNames() {
450 1
		$return = [];
451 1
		$this->query('SHOW TABLES');
452 1
		$i = 0;
453 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

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