Pdo-execute没有返回结果


Pdo execute is not returning results

在使用password_hash()添加加密密码之前,我正在尝试一个简单的代码。

html

<form action="includes/process_login.php" method="post" name="login_form">                      
    Email: <input type="text" name="email" id="email" />
    Password: <input type="password" name="password" id="password"/>
    <input type="submit" value="Connection" /> 
</form>

php

$req = $bdd->prepare('SELECT COUNT(*) AS membre_valide FROM membres WHERE password = :password AND email = :email');
$req->bindValue(':password', $_POST['password'], PDO::PARAM_STR);
$req->bindValue(':email', $_POST['email'], PDO::PARAM_STR);
$data = $req->execute();
$req->closeCursor();
if ( $data['membre_valide'] != 0 ) {
    echo 'Connected';
}
else {
    echo 'Not found';
}

对于$data中的一个echo,它总是返回1。但是当我在mysql中运行SQL行时,它会返回正确的答案。

为什么?

execute函数,但你没有fetch结果:

$exec = $req->execute();
    //$exec will be "true" if it executes; "false" if it doesn't
$data = $req->fetch(PDO::FETCH_ASSOC);
    //fetch() for one row, fetchAll() to retrieve all rows

当您使用execute()发出请求时,返回值只是一个错误代码。因此,基本上,这意味着请求已成功完成。要查找该值,您需要获取该值。您应该尝试$count = (int) $req->fetch(PDO::FETCH_OBJ)->membre_valide;