PHP数据库查询每次都返回false

时间:2014-08-10 01:27:23

标签: php mysql

我在PHP中构建一个非常简单的登录服务,数据库查询每次都返回false。我已经使用phpmyadmin将电子邮件地址添加到我的数据库的users表中,但是当我在字段中输入其中一个电子邮件地址并按下提交时,查询将进入数据库并且每次都返回false。

这是代码:

//queries the database and checks if the email address exists
$result = mysqli_query($connection, "SELECT * FROM 'users' WHERE 'Email'='$email'");
if ($result == FALSE){
die("The account for <i>$email</i> doesn't not exist!");
}

我知道表单中的电子邮件变量是正确的,因为它会被打印为错误。我也知道数据库中的电子邮件地址完全匹配。但是查询只返回false。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

正如@Jared在评论中所说,您在表和列名称上使用'单引号。您可以将它们转换为反引号或只删除它们。

如果您的列,表和数据库名称包含在MySQL的reserved names中,则需要

反引号。

因为您正在使用mysqli_*。我建议您使用预准备语句并使用num_rows代替。例如:

// use prepared statements
$stmt = $connection->prepare('SELECT * FROM `users` WHERE `Email` = ?');
//                                           ^ backticks   ^
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0){
    // found
} else {
    die("The account for <i>$email</i> doesn't not exist!");  
}