php 检查 mysqli_query 是否没有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16996649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if mysqli_query has no values?
提问by Peter Jewicz
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.
如果结果返回为空,我就会遇到麻烦。我尝试过使用几种方法,但如果找不到任何东西,就无法弄清楚如何使其正常工作。
回答by Ananth
Use mysqli_num_rowsto check if any rows were returned or not.
使用mysqli_num_rows检查是否有任何行返回。
回答by Shankar Akunuri
use mysqli_num_rows
like this
mysqli_num_rows
像这样使用
if (mysqli_num_rows($result) == 0) {
echo "email was not found";
} else {
echo "email was found";
}
回答by eric_zyh
if(empty($result))
{
echo("email was not found");
}