PDO声明和阅读结果。 基本

我得到这个函数惠普PDO声明,但我做错了什么:(而且我也知道什么....

它是:foreach($ result as $ row); 这就是为什么我不能读:if($ row ['active'] ==='yes')

但在我的搜索谷歌和堆栈我无法找到写回答。 有人可以看到正确的方法吗? 我仍然对PDO有点新鲜:)

thnx先进。

include ('../../redir/mapping.php'); 
include ($dot2.$RMredir.$RFUserR);

$dbh = new PDO("mysql:host=".$Rhost.";dbname=".$RDB, $RDBsqlR, $RDBpassR);
$stmt = $dbh->prepare("SELECT * FROM `users` WHERE `email`=:email");
$stmt->bindParam(':email', $email,PDO::PARAM_STR);
$stmt->execute();

foreach ($result as $row);

if($stmt->rowCount() === 0)
{
    $emailcheck = 'not_here';
}
else if($stmt->rowCount() === 1)
{
    if($row['active'] === 'yes')
    {
        $emailcheck = 'here_and_active';
    }
    else if($row['active'] === 'no')
    {
        $emailcheck = 'here_and_not_active';
    }
}
else
{
    $emailcheck = 'error';
}

return $emailcheck;

你没有取任何东西。 在你的代码中,$ result是空的:

使用fetch(PDO::FETCH_ASSOC); 获取结果。 尝试:

$dbh = new PDO("mysql:host=".$Rhost.";dbname=".$RDB, $RDBsqlR, $RDBpassR);
$stmt = $dbh->prepare("SELECT * FROM `users` WHERE `email`=:email");
$stmt->bindParam(':email', $email,PDO::PARAM_STR);
$stmt->execute();
$emailcheck = array();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if($stmt->rowCount() === 0){
    $emailcheck[] = 'not_here';
} 
else if($stmt->rowCount() === 1){
    foreach ($result as $row){
        if($row['active'] === 'yes'){
        $emailcheck[] = 'here_and_active';
        }
        else if($row['active'] === 'no'){
        $emailcheck[] = 'here_and_not_active';
        }
    }
}
else{
    $emailcheck[] = 'error';
}

var_dump($emailcheck);
链接地址: http://www.djcxy.com/p/71969.html

上一篇: PDO statement and reading results . basics

下一篇: store multiple pdo connections in an array