skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    SELECT ID 
    FROM tablename 
    WHERE ShowPage = 1 AND 
    DateUpdated BETWEEN 1554345942 AND 1554950742
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search