0
<?php

$sql=mysql_query("SELECT * FROM  table_one INNER JOIN table_two ON table_one.id = table_two.field_id ORDER BY table_one.id ASC LIMIT 0 , 300");
$rowCount=mysql_num_rows($sql);
while($row=mysql_fetch_array($sql)){
    if($rowCount < 1 || $rowCount=="0" || $rowCount == 0 || $rowCount == NULL || $rowCount == false){
        echo "no information retrieved"; 
    }
    else{
        echo "information retrieved";
    }
}

?>

So when setting the condition in the if-else statement I tried several ways to catch the $rowCount value of 0 but it just doesn't seem to work for me.

Does my query need to be using syntax that eliminates null joins or how do I identify when $rowCount = 0 ?

  • If your query doesn't find any record, it returns 0. All the other conditions are not necessary. Put if clause outside while loop and loop result in the else part if you need to display the results. – Nicola Cossu Aug 20 '11 at 23:11
  • Thank you! I wasn't seeing the flow of information properly in the code. – William Morgan Aug 20 '11 at 23:31

2 Answers2

2

The loop while(false) { } will never execute, and that's what you have when there are no rows. You have to put the check for $row_count outside the while-loop:

if ($row_count == 0) { ... no information ... }

else
{
  while ($row = mysql_fetch_array($sql)) { ... }
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

If your Statement has nothing returned, the while loop will never be entered. The expression $row=mysql_fetch_array($sql) will evaluate to true only if there was a row fetched.

Thomas Berger
  • 1,860
  • 13
  • 26