如何检查MySQL查询没有结果

时间:2018-05-17 18:19:17

标签: php mysql

我想检查MySQL查询中没有结果,并向用户返回无结果消息。如何将其与我当前的代码集成?

$result = mysqli_query($con,$sql);

while ($row = mysqli_fetch_array($result)) {

echo '<div class="mydiv">';

if($row['photo']){

    echo "<p>" . '<span class="glyphicon" aria-hidden="true"></span>' . '</p>' . '<p>' . "<img src=http://localhost:8888/example/images/" . $row['photo'] . "><hr>";

}else
echo '<p>' . '<span class="glyphicon" aria-hidden="true"></span>' . $row['message'] . '</p>';
echo '<p>' . '~' . $row['username'] . '</p>';
echo '<p>' . $row['datetime'] . ' ' .'(UTC)' . '</p>';
echo '</div>';

}

我希望在找不到记录时显示“无结果”消息。如何将其集成到现有代码中。请指教。

if ($row = mysql_num_rows($result) == 0){
echo "<h3>There are no result found.</h3>";
}

1 个答案:

答案 0 :(得分:2)

if ($result = mysql_query($sql) && mysql_num_rows($result) > 0) {
    // there are results in $result
    // whatever you want to do with your results
} else {
    // no results
echo "<h3>There are no results found.</h3>";
}