dq1685513999
2016-09-19 22:50 阅读 100
已采纳

if($ result === FALSE) - 没有找到结果时不工作?

This should be simple but I can not get the "if ($result === FALSE)" in the below code to work when no matching records are found. It works just fine if records are found but does not work if no records are found. Can someone point out what I doing wrong?

Thank you Kevin.

$SQL = "SELECT * FROM Master WHERE (Title like '%".$SeachFor."%') or (Keywords like '%".$SeachFor."%') ORDER BY Title" ;
$result = mysqli_query($GLOBALS["connection"], $SQL); 

if ($result === FALSE) { 
    echo 'No results found'; 
} else {
    $num_rows = mysqli_num_rows($result); 
    $Counter = 1;
    $DisplayedCounter = 1;
?>
<?php require_once('displayproduct.php'); ?>
<?php
}
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douwo3665 douwo3665 2016-09-20 09:00

    You could add a || mysql_num_rows() == 0 to your if statement, but something like this would be more ideal.

    if($result === false) {
        trigger_error(mysqli_error($GLOBALS['connection']));
    } else if(mysqli_num_rows($result) === 0){
        echo 'No results found';
    } else {
        $Counter = 1;
        $DisplayedCounter = 1;
    }
    

    This will allow you to see if there are any errors with the SQL query. mysqli_query() only returns false as a boolean upon failure. Otherwise you need to check mysqli_num_rows();

    点赞 评论 复制链接分享

相关推荐