Best way to check if mysql_query returned any results?

To check if a MySQL query returned any results, you can use the mysqli_num_rows function. This function returns the number of rows returned by a SELECT statement, or 0 if no rows were returned.

Here is an example of how to use mysqli_num_rows:


$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
  // output data of each row
  while ($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
} else {
  echo "0 results";

This example performs a SELECT query and checks if any rows were returned. If rows were returned, it fetches each row and prints the values of the "id" and "name" columns. If no rows were returned, it prints "0 results".