我正在学习 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/