PDO跳过if语句

时间:2014-11-28 19:22:02

标签: php pdo

我在这里疯了,为什么他会跳到别的地方并且一直都是假的。 我不明白我做错了什么。

try 
    {
        $query = 'SELECT userID, firstname, surname, email FROM jinx_users WHERE email = :email AND password = :password';
        $this->dbh->beginTransaction();
        $stmt = $this->dbh->prepare($query);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR);
        $stmt->bindParam(':password', $password, PDO::PARAM_STR);
        $stmt->execute();
        if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            return true;
        }else{
            return false;
        }
        $stmt = null;
    } 
    catch (Exception $e) 
    {
        $stmt = null;
        $this->dbh->rollback();
        exit();
    }

1 个答案:

答案 0 :(得分:0)

if语句不是有效语句。除此之外,应该使用Try Catch来检查数据库是否可以成功调用或者可以设置params等。在try / catch中你要避免返回true或false。所以说到我想你想要的是以下内容:

try {
     $query = 'SELECT userID, firstname, surname, email FROM jinx_users WHERE email = :email AND password = :password';
     $this->dbh->beginTransaction();
     $stmt = $this->dbh->prepare($query);
     $stmt->bindParam(':email', $email, PDO::PARAM_STR);
     $stmt->bindParam(':password', $password, PDO::PARAM_STR);
     $stmt->execute();

} catch (Exception $e) {
     var_dump($e->getMessage();
     die();
}

if($stmt->fetch(PDO::FETCH_ASSOC)) {
     return true;
} else {
     return false;
}