15

I have the following code and I would like to know if mysqli_query returned any rows and if so return an error.

$result = mysqli_query($connection, "SELECT * FROM users");
    
if ($result == "") {
    echo "No records found";
} else {
    echo "There is at least one record in the database";
}

I'm having trouble if the results come back as empty. I've tried using several things and can't figure out how to get it work correctly if nothing is found.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Peter Jewicz
  • 664
  • 1
  • 10
  • 27

2 Answers2

16

Use mysqli_num_rows to check if any rows were returned or not.

Ananth
  • 4,227
  • 2
  • 20
  • 26
10

use mysqli_num_rows like this

if (mysqli_num_rows($result) == 0) {
    echo "email was not found";
} else {
    echo "email was found";
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shankar Akunuri
  • 177
  • 3
  • 14