为什么我的SQL语句没有返回结果?

时间:2017-01-12 16:41:52

标签: php mysql sql prepared-statement mariadb

所需的陈述是:

SELECT `id` FROM `user` WHERE `email`='aaa@aaa.com'

...返回单个单元格作为结果。

我的PDO准备好的声明不会返回任何内容。我错过了什么?

$email = "aaa@aaa.com";

$statement = $db->prepare("SELECT `id` FROM `user` WHERE `email`=':email;'");
$statement->execute(['email' => $email]);               
$user = $statement->fetch();
echo $user;

3 个答案:

答案 0 :(得分:1)

报价不应该用于:电子邮件

$statement = $db->prepare("SELECT `id` FROM `user` WHERE `email`=:email");

如@siyual所说

答案 1 :(得分:1)

试试这个:

$email = "aaa@aaa.com";

$stmt = $db->prepare("SELECT `id` FROM `user` WHERE `email`=:email");
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($row)) {
    print_r($row);
}else{
    echo 'no result :/';
}

答案 2 :(得分:-1)

像这样使用它:

$statement = $db->prepare("SELECT `id` FROM `user` WHERE `email`=:email;"); 

$statement->execute(['email' => $email]);

另外,请确保

$db->prepare('select 1');

返回有效的查询结果。