开发者

PHP mysql_num_rows() usage on INNER JOIN query

开发者 https://www.devze.com 2023-03-29 19:07 出处:网络
<?php $sql=mysql_query(\"SELECT * FROMtable_one INNER JOIN table_two ON table_one.id = table_two.field_id ORDER BY table_one.id ASC LIMIT 0 , 300\");
<?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开发者_如何学JAVA 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 ?


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)) { ... }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号