简体   繁体   中英

PHP PDO Prepared Update Statement where =

This is my code:

$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();

What I am trying here is, I want to update users_email_verified row, where emaii, password match the valies + where users_email_verified is set to 0 (not to 1).

0 = Not verified 1 = Verified.

But nothing gets updated in my code, while it is supposed to.

echo count($result); always echo 0 .

No errors are being shown. What is wrong in my code?

1.You need to execute first ( $stmt->execute() ) and then fetch count ( $stmt->rowCount() ).

2. UPDATE query don't return records after successful execution, it just return number of affected rows. So use rowCount() to get number of affected rows.

Check below correct code:-

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM