php - 如何在php中的pdo准备中查找rowCount

标签 php pdo

我正在学习 php 中的 PDO 准备语句。

我有一些错误。

public function check_user_validation($user_email,$user_password)
{
    $salt1 = "5g;;";
    $salt2 = "466UU$%jjh";
    $user_password = hash("sha512",$salt1.$user_password.$salt2);
    $result = $this->con->prepare("select id from user_table where user_email=:user_email&& user_password=:user_password");
    $result->execute([
        ':user_email' => '$user_email',
        ':user_password' => '$user_password',       
    ]);
    $result = $result->rowCount();
    if($result > 0)
    {
        $_SESSION['user_email'] = $user_email;
        header("Location:dashboard.php");
    }
    else
    {
        return FALSE;
    }
}

但是它返回 false。机器人 ID 和密码均属实。我认为 rowCount() 给它 0 值。

最佳答案

PDO::rowCount 不保证适用于 SELECT声明。最好尝试从查询中获取数据。您还遇到一个问题,因为您已将绑定(bind)参数括在单引号中,这意味着查询中的值为 $user_email而不是例如<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="274a4267425f464a574b420944484a" rel="noreferrer noopener nofollow">[email protected]</a> .

$result = $this->con->prepare("select id from user_table where user_email=:user_email&& user_password=:user_password");
$result->execute([
    ':user_email' => $user_email,
    ':user_password' => $user_password,       
]);
if ($result->fetchColumn() !== false)
{
    $_SESSION['user_email'] = $user_email;
    header("Location:dashboard.php");
}
else
{
    return FALSE;
}

关于php - 如何在php中的pdo准备中查找rowCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52454134/

相关文章:

php - Codeigniter/PHP 检查是否可以连接到数据库

php - 通过准备好的语句使用 INSERT INTO 的 PDO

php - 未捕获的 PDOException : could not find driver

php - header 中的 HTTP 内容类型

php - 从 Laravel 5.2 升级到 5.3 时登录时方法 [用户名] 不存在

php - WordPress .htaccess 不适用于 rewriterule

php - PDO 查询总是返回 1 或 true

javascript - 在 WordPress 的 javascript 中留下 API key 等私有(private)信息是否安全?

php - 如何使用pdo mysql在插入记录期间检查字段数据类型是否一致

php - 从两个表中选择而不重复所有值