fetchAll是一个非对象

时间:2016-01-11 12:20:15

标签: pdo

我的fetchAll是这个代码的非对象错误:

var_dump($_POST);

include('config_path.php');
$bdd->query("SET NAMES 'utf8'");
$req = $bdd->prepare("SELECT * FROM admin WHERE nom_uti_admin= :username AND mdp_admin=:password");
$result = $req->execute(array(
    'username' => $_POST['username'],
    'password' => $_POST['password']
));
$connect = $result->fetchAll();

var_dump返回最后一页表单的正确值... 你知道错误吗? 请求与phpmyadmin中的真值有效!

由于

编辑:

以下代码验证:

if(isset($_POST['username']) && isset($_POST['password'])){
    if(count($connect) == 1){
        $_SESSION['auth'] = $cle;
        $_SESSION['username'] = $_POST['username'];
        header("Location: admin/afficher_spec.php");
    }else if(count($connect) == 0){
        echo 'nom d\'utilisateur et/ou mot de passe incorrect !';
    }
}else {
    echo 'Veuillez remplir tous les champs ';
}

1 个答案:

答案 0 :(得分:0)

如果要使用foreach循环遍历它,则只使用fetchAll。在您的情况下,您只需要返回一条记录,因此您需要使用fetch

$connect = $result->fetch(PDO::FETCH_ASSOC); 
if(count($connect) == 1){$_SESSION['auth'] = $cle;
        $_SESSION['username'] = $_POST['username'];
        header("Location: admin/afficher_spec.php");
    }else if(count($connect) != 1){
        echo 'nom d\'utilisateur et/ou mot de passe incorrect !';
    }