Null values from query functions.

From: Peter (BOUGHTONP)24 Mar 15:40
To: Matt 1 of 8
Hey Matt,

I'm getting errors with PHP 7.4.15 and MariaDB 10.5.9 on Debian.

For example, function thread_has_attachments in include/thread.inc.php - line 1365 should return false but doesn't.
Same situation for stats_get_most_popular_birthday in include/stats.inc.php line 1499

In both cases, the queries correctly return no values, but the if condition is failing:

if (!($result = $db->query($sql))) return false;

Is this a PHP change, a DB driver issue, or something else?

From: Peter (BOUGHTONP)24 Mar 15:51
To: Peter (BOUGHTONP) 2 of 8
Seems to work if I add "->num_rows", i.e:
if (!($result = $db->query($sql))->num_rows) return false;

The docs suggest this works in PHP 5, so a mass search-replace won't break backwards compatibility?

https://www.php.net/manual/en/mysqli-result.num-rows.php

EDITED: 24 Mar 15:54 by BOUGHTONP
From: Peter (BOUGHTONP)24 Mar 16:57
To: Peter (BOUGHTONP) 3 of 8
> Seems to work if I add "->num_rows", i.e:

Except for UPDATE queries which return booleans instead of objects, such as in the user_logon function.

Stupid PHP.

Also, checking a few of the 391 matches, some of them already have additional num_rows checks on separate rows.

Not sure if it'll be easier to click every link and fix them as they crop up, but I haven't had lunch yet so I'm doing that first.

From: ANT_THOMAS24 Mar 21:30
To: Peter (BOUGHTONP) 4 of 8
What was for lunch?
From: Peter (BOUGHTONP)25 Mar 13:09
To: ANT_THOMAS 5 of 8
A traditional Italian dish.
From: koswix26 Mar 15:34
To: Peter (BOUGHTONP) 6 of 8
What was on top of it?
From: Peter (BOUGHTONP)27 Mar 17:01
To: koswix 7 of 8
Mature cheddar cheese - if you believe the packaging - but always an insubstantial amount, and the necessary stirring prevents any noticable taste that those few grams might impart.
From: Matt28 Mar 00:09
To: Peter (BOUGHTONP) 8 of 8
$db->query (which is a wrapper around mysqli::query) can return false, so adding a call to ->num_rows to it won't always work (as you now already know), but you could check for $result->num_rows afterwards, e.g.:
 
Code: 
if (!($result = $db->query($sql))) return false;

if ($result->num_rows === 0) return false;
EDITED: 28 Mar 00:10 by MATT