Checking if mysqli_query has no values? [closed]

2019-02-13 12:11发布

问题:

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.

回答1:

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



回答2:

Use this.

$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");
}


回答3:

use mysqli_num_rows like this

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

hope it will help u..



回答4:

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


标签: php mysql mysqli