Home » Php » php – Checking if mysqli_query has no values?

php – Checking if mysqli_query has no values?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have the following code for a sign up form. I want to check the email to see if it’s already logged in the database, and if so return an error.

$name = $_POST['name'];
$email = $_POST['email'];

$result = mysqli_query($connection,"SELECT * FROM users WHERE email='$email'");

if($result=="")
{
echo("email was not found");
}
else
{
    echo("email was found");
}

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.

How to&Answers:

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

Answer:

use mysqli_num_rows like this

if (mysqli_num_rows($result) == 0) {
    echo "email was not found";
} else {
    echo "email was found";
}

Answer:

if(empty($result))
{
    echo("email was not found");
}