Passed
Push — master ( ae3b0e...197bfe )
by Joe
03:05
created

Db::num_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_select_db() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

47
		mysqli_select_db(/** @scrutinizer ignore-type */ $this->linkId, $database);
Loading history...
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mysqli seems to be never defined.
Loading history...
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 ($port != '') {
91
                $this->linkId->real_connect($host, $user, $password, $database, $port);
92
            } else {
93 26
                $this->linkId->real_connect($host, $user, $password, $database);
94
            }			
95 26
			$this->linkId->set_charset($this->characterSet);
96 26
			if ($this->linkId->connect_errno) {
97
				$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error);
98
				return 0;
99
			}
100
		}
101 26
		return $this->linkId;
102
	}
103
104
	/**
105
	 * Db::disconnect()
106
	 * @return bool
107
	 */
108 1
	public function disconnect()
109
	{
110 1
		$return = method_exists($this->linkId, 'close') ? $this->linkId->close() : false;
111 1
		$this->linkId = 0;
112 1
		return $return;
113
	}
114
115
	/**
116
	 * @param $string
117
	 * @return string
118
	 */
119 2
	public function real_escape($string = '')
120
	{
121 2
		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...
122
			return $this->escape($string);
123
		}
124 2
		return mysqli_real_escape_string($this->linkId, $string);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer and resource; however, parameter $link of mysqli_real_escape_string() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

124
		return mysqli_real_escape_string(/** @scrutinizer ignore-type */ $this->linkId, $string);
Loading history...
125
	}
126
127
	/**
128
	 * discard the query result
129
	 * @return void
130
	 */
131 1
	public function free()
132
	{
133 1
		if (is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
134
			@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

134
			/** @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...
135
		}
136 1
		$this->queryId = 0;
137
	}
138
139
	/**
140
	 * Db::queryReturn()
141
	 *
142
	 * Sends an SQL query to the server like the normal query() command but iterates through
143
	 * any rows and returns the row or rows immediately or FALSE on error
144
	 *
145
	 * @param mixed $query SQL Query to be used
146
	 * @param string $line optionally pass __LINE__ calling the query for logging
147
	 * @param string $file optionally pass __FILE__ calling the query for logging
148
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
149
	 */
150 1
	public function queryReturn($query, $line = '', $file = '')
151
	{
152 1
		$this->query($query, $line, $file);
153 1
		if ($this->num_rows() == 0) {
154 1
			return false;
155 1
		} elseif ($this->num_rows() == 1) {
156 1
			$this->next_record(MYSQLI_ASSOC);
157 1
			return $this->Record;
158
		} else {
159 1
			$out = [];
160 1
			while ($this->next_record(MYSQLI_ASSOC)) {
161 1
				$out[] = $this->Record;
162
			}
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
	{
179 1
		return $this->queryReturn($query, $line, $file);
180
	}
181
182
	/**
183
	 * creates a prepaired statement from query
184
	 *
185
	 * @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 = ?
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
	 * @param string $line
188
	 * @param string $file
189
	 */
190 1
	public function prepare($query, $line = '', $file = '')
191
	{
192 1
		if (!$this->connect()) {
193
			return 0;
194
		}
195 1
		$haltPrev = $this->haltOnError;
0 ignored issues
show
Unused Code introduced by
The assignment to $haltPrev is dead and can be removed.
Loading history...
196 1
		$this->haltOnError = 'no';
197 1
		$start = microtime(true);
198 1
		$prepare = mysqli_prepare($this->linkId, $query);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_prepare() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

198
		$prepare = mysqli_prepare(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
199 1
		$this->addLog($query, microtime(true) - $start, $line, $file);
200 1
		return $prepare;
201
	}
202
203
	/**
204
	 * Db::query()
205
	 *
206
	 *  Sends an SQL query to the database
207
	 *
208
	 * @param mixed $queryString
209
	 * @param string $line
210
	 * @param string $file
211
	 * @return mixed 0 if no query or query id handler, safe to ignore this return
212
	 */
213 10
	public function query($queryString, $line = '', $file = '')
214
	{
215
		/* No empty queries, please, since PHP4 chokes on them. */
216
		/* The empty query string is passed on from the constructor,
217
		* when calling the class without a query, e.g. in situations
218
		* like these: '$db = new db_Subclass;'
219
		*/
220 10
		if ($queryString == '') {
221 1
			return 0;
222
		}
223 10
		if (!$this->connect()) {
224
			return 0;
225
			/* we already complained in connect() about that. */
226
		}
227 10
		$haltPrev = $this->haltOnError;
228 10
		$this->haltOnError = 'no';
229
		// New query, discard previous result.
230 10
		if (is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
231
			$this->free();
232
		}
233 10
		if ($this->Debug) {
234 1
			printf("Debug: query = %s<br>\n", $queryString);
235
		}
236 10
		if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) {
237
			$this->log($queryString, $line, $file);
238
		}
239 10
		$tries = 3;
240 10
		$try = 0;
241 10
		$this->queryId = false;
242 10
		while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) {
243 10
			$try++;
244 10
			if ($try > 1) {
245
				@mysqli_close($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_close() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

245
				@mysqli_close(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
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

245
				/** @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...
246
				$this->connect();
247
			}
248 10
			$start = microtime(true);
249 10
            $onlyRollback = true;
250 10
            $fails = 0;
251 10
            while ($fails < 10 && false === $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT)) {
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

251
            while ($fails < 10 && false === $this->queryId = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $queryString, MYSQLI_STORE_RESULT)) {
Loading history...
252
                $fails++; 
253
                if (@mysqli_errno($this->linkId) == 3101)
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_errno() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

253
                if (@mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId) == 3101)
Loading history...
254
                    usleep(500000); // half a second
255
                else
256
                    $onlyRollback = false;
257
            } 
258 10
            if ($onlyRollback === true && false === $this->queryId) {
259
                myadmin_log('myadmin', 'error', 'Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up', __LINE__, __FILE__);
0 ignored issues
show
Bug introduced by
The function myadmin_log was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

259
                /** @scrutinizer ignore-call */ 
260
                myadmin_log('myadmin', 'error', 'Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up', __LINE__, __FILE__);
Loading history...
260
            }
261 10
 			$this->addLog($queryString, microtime(true) - $start, $line, $file);
262 10
			$this->Row = 0;
263 10
			$this->Errno = @mysqli_errno($this->linkId);
264 10
			$this->Error = @mysqli_error($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_error() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

264
			$this->Error = @mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
265 10
			if ($try == 1 && (null === $this->queryId || $this->queryId === false)) {
266
				$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file);
267
			}
268
		}
269 10
		$this->haltOnError = $haltPrev;
270 10
		if (null === $this->queryId || $this->queryId === false) {
271
			$this->halt('', $line, $file);
272
		}
273
274
		// Will return nada if it fails. That's fine.
275 10
		return $this->queryId;
276
	}
277
278
	/**
279
	 * @return array|null|object
280
	 */
281 1
	public function fetchObject()
282
	{
283 1
		$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

283
		$this->Record = @mysqli_fetch_object(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
284 1
		return $this->Record;
285
	}
286
287
	/* public: walk result set */
288
289
	/**
290
	 * Db::next_record()
291
	 *
292
	 * @param mixed $resultType
293
	 * @return bool
294
	 */
295 6
	public function next_record($resultType = MYSQLI_BOTH)
296
	{
297 6
		if ($this->queryId === false) {
0 ignored issues
show
introduced by
The condition $this->queryId === false is always false.
Loading history...
298
			$this->haltmsg('next_record called with no query pending.');
299
			return 0;
300
		}
301
302 6
		$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

302
		$this->Record = @mysqli_fetch_array(/** @scrutinizer ignore-type */ $this->queryId, $resultType);
Loading history...
303 6
		++$this->Row;
304 6
		$this->Errno = mysqli_errno($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_errno() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

304
		$this->Errno = mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
305 6
		$this->Error = mysqli_error($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_error() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

305
		$this->Error = mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
306
307 6
		$stat = is_array($this->Record);
308 6
		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...
309
			$this->free();
310
		}
311 6
		return $stat;
312
	}
313
314
	/**
315
	 * switch to position in result set
316
	 *
317
	 * @param integer $pos the row numbe starting at 0 to switch to
318
	 * @return bool whetherit was successfu or not.
319
	 */
320 1
	public function seek($pos = 0)
321
	{
322 1
		$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

322
		$status = @mysqli_data_seek(/** @scrutinizer ignore-type */ $this->queryId, $pos);
Loading history...
323 1
		if ($status) {
324 1
			$this->Row = $pos;
325
		} else {
326 1
			$this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__);
327
			/* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */
328 1
			$rows = $this->num_rows();
329 1
			@mysqli_data_seek($this->queryId, $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

329
			/** @scrutinizer ignore-unhandled */ @mysqli_data_seek($this->queryId, $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...
330 1
			$this->Row = $rows;
331 1
			return false;
332
		}
333 1
		return true;
334
	}
335
336
	/**
337
	 * Initiates a transaction
338
	 *
339
	 * @return bool
340
	 */
341 26
	public function transactionBegin()
342
	{
343 26
		if (version_compare(PHP_VERSION, '5.5.0') < 0) {
344
			return true;
345
		}
346 26
		if (!$this->connect()) {
347
			return 0;
348
		}
349 26
		return mysqli_begin_transaction($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_begin_transaction() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

349
		return mysqli_begin_transaction(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
350
	}
351
352
	/**
353
	 * Commits a transaction
354
	 *
355
	 * @return bool
356
	 */
357 1
	public function transactionCommit()
358
	{
359 1
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) {
360
			return true;
361
		}
362 1
		return mysqli_commit($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_commit() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

362
		return mysqli_commit(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
363
	}
364
365
	/**
366
	 * Rolls back a transaction
367
	 *
368
	 * @return bool
369
	 */
370 26
	public function transactionAbort()
371
	{
372 26
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) {
373
			return true;
374
		}
375 26
		return mysqli_rollback($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_rollback() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

375
		return mysqli_rollback(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
376
	}
377
378
	/**
379
	 * This will get the last insert ID created on the current connection.  Should only be called after an insert query is
380
	 * run on a table that has an auto incrementing field.  $table and $field are required, but unused here since it's
381
	 * unnecessary for mysql.  For compatibility with pgsql, the params must be supplied.
382
	 *
383
	 * @param string $table
384
	 * @param string $field
385
	 * @return int|string
386
	 */
387 2
	public function getLastInsertId($table, $field)
388
	{
389 2
		if (!isset($table) || $table == '' || !isset($field) || $field == '') {
390
			return -1;
391
		}
392
393 2
		return @mysqli_insert_id($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_insert_id() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

393
		return @mysqli_insert_id(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
394
	}
395
396
	/* public: table locking */
397
398
	/**
399
	 * Db::lock()
400
	 * @param mixed  $table
401
	 * @param string $mode
402
	 * @return bool|int|\mysqli_result
403
	 */
404 1
	public function lock($table, $mode = 'write')
405
	{
406 1
		$this->connect();
407 1
		$query = 'lock tables ';
408 1
		if (is_array($table)) {
409 1
			foreach ($table as $key => $value) {
410 1
				if ($key == 'read' && $key != 0) {
411
					$query .= "$value read, ";
412
				} else {
413 1
					$query .= "$value $mode, ";
414
				}
415
			}
416 1
			$query = mb_substr($query, 0, -2);
417
		} else {
418 1
			$query .= "$table $mode";
419
		}
420 1
		$res = @mysqli_query($this->linkId, $query);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

420
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
421 1
		if (!$res) {
422
			$this->halt("lock($table, $mode) failed.");
423
			return 0;
424
		}
425 1
		return $res;
426
	}
427
428
	/**
429
	 * Db::unlock()
430
	 * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error
431
	 * @return bool|int|\mysqli_result
432
	 */
433 2
	public function unlock($haltOnError = true)
434
	{
435 2
		$this->connect();
436
437 2
		$res = @mysqli_query($this->linkId, 'unlock tables');
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

437
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, 'unlock tables');
Loading history...
438 2
		if ($haltOnError === true && !$res) {
439
			$this->halt('unlock() failed.');
440
			return 0;
441
		}
442 2
		return $res;
443
	}
444
445
	/* public: evaluate the result (size, width) */
446
447
	/**
448
	 * Db::affectedRows()
449
	 * @return int
450
	 */
451 2
	public function affectedRows()
452
	{
453 2
		return @mysqli_affected_rows($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_affected_rows() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

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

453
		return @mysqli_affected_rows(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
454
	}
455
456
	/**
457
	 * Db::num_rows()
458
	 * @return int
459
	 */
460 6
	public function num_rows()
461
	{
462 6
		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

462
		return @mysqli_num_rows(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
463
	}
464
465
	/**
466
	 * Db::num_fields()
467
	 * @return int
468
	 */
469 1
	public function num_fields()
470
	{
471 1
		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

471
		return @mysqli_num_fields(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
472
	}
473
474
	/**
475
	 * gets an array of the table names in teh current datase
476
	 *
477
	 * @return array
478
	 */
479 1
	public function tableNames()
480
	{
481 1
		$return = [];
482 1
		$this->query('SHOW TABLES');
483 1
		$i = 0;
484 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

484
		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...
485 1
			$return[$i]['table_name'] = $info[0];
486 1
			$return[$i]['tablespace_name'] = $this->database;
487 1
			$return[$i]['database'] = $this->database;
488 1
			++$i;
489
		}
490 1
		return $return;
491
	}
492
}
493
494
/**
495
 * @param $result
496
 * @param $row
497
 * @param int|string $field
498
 * @return bool
499
 */
500
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
501
function mysqli_result($result, $row, $field = 0) {
502
	if ($result === false) return false;
503
	if ($row >= mysqli_num_rows($result)) return false;
504
	if (is_string($field) && !(mb_strpos($field, '.') === false)) {
505
		$tField = explode('.', $field);
506
		$field = -1;
507
		$tFields = mysqli_fetch_fields($result);
508
		for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) {
509
			if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) {
510
				$field = $id;
511
				break;
512
			}
513
		}
514
		if ($field == -1) return false;
515
	}
516
	mysqli_data_seek($result, $row);
517
	$line = mysqli_fetch_array($result);
518
	return isset($line[$field]) ? $line[$field] : false;
519
}
520
*/
521