PDO statement and reading results . basics

I got this function whit PDO statement but i'm doing something wrong:( And I also know what....

it's the : foreach ($result as $row); thats why i can't read : if($row['active'] === 'yes')

But in mine search on google and on stack i cant find the write answer. Could somebody see what the right way is???? I'm still a little bit new to PDO :)

thnx in advanced.

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;

You aren't fetching anything. In your code $result is empty:

use fetch(PDO::FETCH_ASSOC); to fetch the result. try:

$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/71970.html

上一篇: 在安装Django之前是否需要卸载Django 1.3?

下一篇: PDO声明和阅读结果。 基本