我有以下PHP代码段,当WHERE子句不匹配时,它应该返回false。 对于ex: 我在数据库中没有选择器值和 ghddd ,因此我认为更新不应该发生,我应该失败,但我一直成功。我在哪里做错了?
if(isset($_COOKIE['authUser'])) {
$cookie_contents = $_COOKIE['authUser'];
//list($selector, $plain_token) = explode(':', $cookie_contents);
$selector = "ghddd";
//Empty the values
$query = $dbh->prepare("UPDATE auth_tokens SET token=:token, expires=:expires WHERE selector = '".$selector."'");
if($query->execute(array(
":token" => "",
":expires" => ""
))) {
echo 'Success';
}
else {
echo 'Failure';
}
// Delete the authentication cookie
//setcookie('authUser', "", time() - 1);
}
答案 0 :(得分:1)
如果要检查UPDATE
查询是否实际更新了一行或多行,可以在执行查询后使用PDOStatement::rowCount。
答案 1 :(得分:0)
一些观察:
1.)您绑定了expires
和token
但未绑定selector
。
2.)您可能希望删除包含命名参数的数组前面的冒号(:)。
if(isset($_COOKIE['authUser'])) {
$cookie_contents = $_COOKIE['authUser'];
$selector = "ghddd";
$query = $dbh->prepare("UPDATE auth_tokens
SET token=:token,
expires=:expires WHERE
selector=:selector");
// $query->execute(...) IS EXPECTED TO RETURN A BOOLEAN TRUE OR FALSE.
if( $query->execute(array(
"token" => "", //<== COLON (:) REMOVED
"expires" => "", //<== COLON (:) REMOVED
"selector" => $selector
))) {
echo 'Success'; //<== EXPECTING BOOLEAN 'TRUE' FROM $query->execute
}
else {
echo 'Failure'; //<== EXPECTING BOOLEAN 'FALSE' FROM $query->execute
}
}