0

Does anyone know why the following mysqli_num_rows is NOT returning zero when there are no results? Instead of displaying the '0' character it simply displays nothing. Totally baffled. EDIT << msqli_num_rows is working correctly and display a number when there ARE results, it just isn't when there aren't any results...

   $result = mysqli_query($con,"
           SELECT LastName 
           AS sLast, FirstName
                AS sFirst
           FROM students
                 LEFT JOIN
                 (SELECT id, name
                 FROM employers)
                 a ON a.id = $e_id
           WHERE WK1 = a.name");
    while($row = mysqli_fetch_array($result)) { 
    $wk1 = mysqli_num_rows($result);
    echo $row['sLast'] .", ".$row['sFirst']."<br>";
    }
Phil Howell
  • 79
  • 2
  • 8
  • it may be related http://stackoverflow.com/questions/3325256/if-mysql-num-rows-equals-to-zero-is-not-working EDIT sorry, it may not be, i think i could have misunderstood – Francesco Zaffaroni Jun 19 '14 at 11:27

2 Answers2

3

When there are no rows there is nothing to fetch so your loop never runs. Try getting num_rows first.

$wk1 = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {     
    echo $row['sLast'] .", ".$row['sFirst']."<br>";
}
Jim
  • 22,354
  • 6
  • 52
  • 80
-1
  1. mysqli_query will return result if it found record matched.
  2. It return false when no record found , so in false case there would no return 0 .
  • `mysqli_query` will only return false on an error. If there are no rows but the query is successful you will get a `mysqli_result` object. – Jim Jun 19 '14 at 11:35
  • check please http://us3.php.net//manual/en/mysqli.query.php in Return Values topic. I think it return false when no record found – Sajid Ali Khan Jun 19 '14 at 11:51
  • From the link `Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.` returning 0 rows is not a failure. You can try this out yourself. – Jim Jun 19 '14 at 11:52