Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if mysqli_query returned any values? [closed]

Tags:

php

mysqli

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.

like image 376
Peter Jewicz Avatar asked Jun 08 '13 06:06

Peter Jewicz


2 Answers

use mysqli_num_rows like this

if (mysqli_num_rows($result) == 0) {
    echo "email was not found";
} else {
    echo "email was found";
}
like image 102
Shankar Akunuri Avatar answered Oct 13 '22 22:10

Shankar Akunuri


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

like image 32
Ananth Avatar answered Oct 14 '22 00:10

Ananth