[英]PHP PDO Prepared Update Statement where =
這是我的代碼:
$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,密碼與有效值+匹配,其中users_email_verified
設置為0(而不是1)。
0 =未驗證1 =已驗證。
但是我的代碼中沒有任何內容得到更新,盡管應該更新。
echo count($result);
總是echo 0
。
沒有錯誤顯示。 我的代碼有什么問題?
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.