00 Votes

PHP: Catch empty MySQL result

Question by Guest | 2012-11-22 at 17:20

In one of my PHP scripts, it may happen that the result of a MySQL query is empty.

Silly, if then the script just keeps running. Therefore, I would like to know - before outputting the data - if my MySQL result delivers an empty set or data is available. How can I implement it?

ReplyPositiveNegative
1Best Answer1 Vote

You can simply use the function mysqli_num_rows(). This function returns the number of records (rows) containing your result:

$res = mysqli_query($db, "SELECT * FROM tab WHERE id > 80");
 
if (mysqli_num_rows($res) == 0) {
  echo 'There were no matches.';
} else {
  // output data
}

In this way, you can design your code, to show the user, if there were no hits.
Last update on 2020-09-15 | Created on 2012-11-24

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.