If I use this in PHP to count records, it gives a count of 1 when there are, in fact, no records. However, when I run the same query in HeidiSQL, phpmyadmin or any other, it gives the proper 0. Why the discrepancy?
if ($result = $mysqli->query($Query)) :
return $result->num_rows;
$MySQLError = ($mysqli->connect_errno) ? mysqli_error($mysqli) : "";
$result->close();
$mysqli->close();
if ($MySQLError) return $MySQLError;
endif;
$Query, in this case, contains:
SELECT COUNT(ID) AS UpdateCount
FROM tablename
WHERE ShowPage = 1 AND
DateUpdated BETWEEN 1554345942 AND 1554950742
2
Answers
While working on something else, the answer to this problem suddenly hit me and it's obvious now that I see it. I was using COUNT(ID) in the query but also using $result->num_rows in the programming. Simply removing the COUNT(ID) did the trick.
This issue might occur if another database session has not yet been committed. Making sure that there are no other database sessions which have not been committed solves this problem.