user2192094
user2192094

Reputation: 337

How to check for no result in MySQL query

I want to check for no result in MySQL query and return a no result message to the user. How do I integrate it with my current code?

$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>';

}

I want a "no result" message to show up when there are no records found. How do I integrate it in my existing code. Please advise.

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

Upvotes: 1

Views: 47

Answers (1)

James Lingham
James Lingham

Reputation: 439

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>";
}

Upvotes: 2

Related Questions