dourui7186 2016-11-10 14:23
浏览 57
已采纳

Pdo更新不应该返回false

I have the following PHP snippet which should return false when the WHERE clause doesn't match. For ex: I don't have any selector value with ghddd in the database so it is supposed that the update shouldn't take place and I should get Failure but I keep getting Success. Where am I doing wrong?

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);
    }
  • 写回答

2条回答 默认 最新

  • dragon321723 2016-11-10 15:00
    关注

    If you want check if your UPDATE query has actually updated one or more rows, you can use PDOStatement::rowCount after the execution of the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?