为什么这个获取函数没有返回任何东西?

时间:2018-02-11 20:58:06

标签: php pdo

我正在尝试使用PDO创建登录系统但由于某种原因我的fetch函数没有返回任何内容。我已尝试使用print_r打印出抓取内容,但它无法正常工作。

$sql = "SELECT * FROM users WHERE user_email=?";
$stmt = $db->prepare($sql);
$stmt->execute(array($email));
$result = $stmt->fetchAll();
$rowCount = $stmt->rowCount();

if($rowCount < 1) {
    echo "error";
    // header("Location: ../login.php?login=email_error");
    exit();
} else {
    if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {}
        $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
        if($hashedPwdCheck == false) {
            exit();
        } elseif($hashedPwdCheck == true) {
            // Log in the user
            $_SESSION['id'] = $row['user_id'];
            $_SESSION['email'] = $row['user_email'];
            echo "no, here!";
            exit();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

正如所指出的 - 你两次获取数据,你可以注释掉这一行......

$result = $stmt->fetchAll();

稍后您将获取用户的特定行(对fetch()的调用)

或者更改以下部分......

} else {
        $hashedPwdCheck = password_verify($pwd, $result[0]['user_pwd']);
        if($hashedPwdCheck == false) {
            exit();
        } elseif($hashedPwdCheck == true) {
            // Log in the user
            $_SESSION['id'] = $result[0]['user_id'];
            $_SESSION['email'] = $result[0]['user_email'];
            echo "no, here!";
            exit();
        }
    }
}

因此,您回顾fetchAll()中提取的数据。当fetchAll()返回记录数组时,您需要使用$result[0]来引用结果集中的第一行。

相关问题