Post by boopbeepforum » Sun Jun 20, 2021 10:12 pm

I'm comparing mysqli in 3.x and older 1.5.6.x and see a difference here between(isset($query->num_rows)) and ($query instanceof \mysqli_result):

Code: Select all

//1.5.6.x
if (isset($query->num_rows)) {
	$data = array();

	while ($row = $query->fetch_assoc()) {
		$data[] = $row;
	}

	$result = new stdClass();
	$result->num_rows = $query->num_rows;
	$result->row = isset($data[0]) ? $data[0] : array();
	$result->rows = $data;

	unset($data);

	$query->close();

	return $result;
} else{
	return true;
}

Code: Select all

//3.x
if ($query instanceof \mysqli_result) {
	$data = array();

	while ($row = $query->fetch_assoc()) {
		$data[] = $row;
	}

	$result = new \stdClass();
	$result->num_rows = $query->num_rows;
	$result->row = isset($data[0]) ? $data[0] : array();
	$result->rows = $data;

	$query->close();

	return $result;
} else {
	return true;
}
for 1.5.6.x , if nothing is found in the database and the return is true, then wouldn't there be problems where for example this produces a warning?

Code: Select all

$my_query = $this->db->query($sql);
//nothing was found so then is $my_query = true ?

if ($my_query->num_rows) echo 'hi';
//Warning: Attempt to read property "num_rows" on bool
In 3.x, even if nothing is found in the database, it will still return num_rows = 0, and also empty arrays in row and rows?
It will only return true if instanceof \mysqli_result is false, whatever this means?

Is there any benefit of updating 1.5.6.x to use($query instanceof \mysqli_result) and instead of the (isset($query->num_rows))? Or will it break?

I don't know what a namespace is and how it works with the backslash.

Code: Select all

//Since my 1.5.6.x uses:
final class DBMySQLi {
	private $link;
	public function __construct($hostname, $username, $password, $database) {
		$this->link = new mysqli($hostname, $username, $password, $database);

//While 3.x uses:
namespace DB;
final class MySQLi {
	private $connection;
	public function __construct($hostname, $username, $password, $database, $port = '3306') {
		$this->connection = new \mysqli($hostname, $username, $password, $database, $port);
Then, should I update 1.5.6.x to this?
($query instanceof dbmysqli_result)
or:
($query instanceof mysqli_result)
from this:
($query instanceof \mysqli_result)

Newbie

Posts

Joined
Tue May 18, 2021 9:40 pm

Post by straightlight » Sun Jun 20, 2021 11:17 pm

You should upgrade to the latest release. These methods are way passed their times.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by boopbeepforum » Mon Jun 21, 2021 12:32 am

I understand your opinion, but I am using 1.5.6.x and am not able to change versions.

I just would like some advice whether or not I should change:
(isset($query->num_rows))
to
($query instanceof \mysqli_result)

what would be the behavior and benefit of doing so,
and whether I need to replace the backslash with db or remove it.

Newbie

Posts

Joined
Tue May 18, 2021 9:40 pm

Post by ADD Creative » Mon Jun 21, 2021 2:49 am

boopbeepforum wrote:
Mon Jun 21, 2021 12:32 am
I understand your opinion, but I am using 1.5.6.x and am not able to change versions.

I just would like some advice whether or not I should change:
(isset($query->num_rows))
to
($query instanceof \mysqli_result)

what would be the behavior and benefit of doing so,
and whether I need to replace the backslash with db or remove it.
There will be no difference. If query returns a mysqli_result object both will be true. This is because the returned query object will always have a num_rows property, so isset will be true even if num_rows is zero.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by boopbeepforum » Mon Jun 21, 2021 4:33 am

ADD Creative wrote:
Mon Jun 21, 2021 2:49 am
There will be no difference. If query returns a mysqli_result object both will be true. This is because the returned query object will always have a num_rows property, so isset will be true even if num_rows is zero.
Ah, thank you, I misunderstood that it would return false, empty, or undefined. So if it always has num_rows when query isn't found in the database, then there won't be a warning for reading property of bool.

Is there any validation or authentication benefit of ($query instanceof mysqli_result) instead of the isset num_rows?

Newbie

Posts

Joined
Tue May 18, 2021 9:40 pm

Post by ADD Creative » Mon Jun 21, 2021 5:22 pm

I can't think of any benefit, but I guess instanceof is probably what is really being tested for, which is why it was changed. It may also be fractionally quicker.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom
Who is online

Users browsing this forum: No registered users and 73 guests