PHP PDO准备更新语句where =

时间:2017-08-25 07:54:07

标签: php mysql pdo

这是我的代码:

$stmt = $conn->prepare("UPDATE site_users SET users_email_verified = :users_email_verified WHERE users_email = :users_email AND users_password = :users_password and users_email_verified = :users_email_not_verified ");

$stmt->bindParam(':users_email_not_verified', $users_email_not_verified,PDO::PARAM_STR);
$stmt->bindParam(':users_email_verified', $users_email_verified,PDO::PARAM_STR);
$stmt->bindParam(':users_email',$_GET["email"],PDO::PARAM_STR);
$stmt->bindParam(':users_password',$_GET["token"],PDO::PARAM_STR);

$users_email_verified = 1;
$users_email_not_verified = 0;

// The next 2 lines are supposed to count total number of rows effected
$result = $stmt->fetchAll();
echo count($result);
$stmt->execute();

我在这里尝试的是,我想更新users_email_verified行,其中emaii,密码与valies +匹配,其中users_email_verified设置为0(不是1)。

0 =未经验证 1 =已验证。

但我的代码中没有任何内容更新,而它应该是。

echo count($result);始终回显0

没有显示任何错误。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:3)

1.您需要 先执行 $stmt->execute()),然后 获取计数 $stmt->rowCount())。

2. UPDATE查询在成功执行后不返回记录,只返回受影响的行数。因此,请使用rowCount()获取受影响的行数。

检查以下正确的代码: -

$stmt = $conn->prepare("UPDATE site_users SET users_email_verified = :users_email_verified WHERE users_email = :users_email AND users_password = :users_password and users_email_verified = :users_email_not_verified ");

$users_email_verified = 1;
$users_email_not_verified = 0;

$stmt->bindParam(':users_email_not_verified', $users_email_not_verified,PDO::PARAM_STR);
$stmt->bindParam(':users_email_verified', $users_email_verified,PDO::PARAM_STR);
$stmt->bindParam(':users_email',$_GET["email"],PDO::PARAM_STR);
$stmt->bindParam(':users_password',$_GET["token"],PDO::PARAM_STR);



// The next 2 lines are supposed to count total number of rows effected

$stmt->execute();
$result = $stmt->rowCount();
echo $result;