PDO声明和阅读结果。基本

时间:2015-03-18 22:47:26

标签: php mysql pdo

我得到了这个函数whit PDO语句,但我做错了什么:( 而且我也知道......

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

但是我在google和堆栈上的搜索中找不到写回答。 有人能看出正确的方法吗???? 我对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;

1 个答案:

答案 0 :(得分:1)

你没有取得任何东西。在您的代码中,$ 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);