dpsx99068 2013-07-31 13:22
浏览 12
已采纳

PDO更新声明不起作用

I've browsed the web quite a bit before asking here; I noticed that some people have the same problem as me, but none of the answers that were given to the others didn't solve my problem, so....

I have a basic PDO Update Statement inside a public function:

 public function editRank($name, $rank){
$query = "UPDATE `chat_mod` SET `chat_mod_rank` = :rank WHERE `chat_mod_ign` = :username";
$prepare = $this->_db->prepare($query);
$array = array(
    ':rank'     => $rank,
    ':username' => $name
);

try {
    $prepare->execute($array);
} catch (PDOException $e){
    echo 'Error: ' . $e->getMessage();
    return false;
}
return true; // If no PDO Exception is thrown..

}

No exception is thrown, so the function always returns true; but the rows are not being updated. Yes, I've checked that the rows are named properly and the values are not nulls.

Thanks, Tom.

P.S Other queries like Select, Add & Delete work fine.

  • 写回答

1条回答 默认 最新

  • dongmubi4444 2013-07-31 13:40
    关注

    You are catching PDO exceptions but did you tell PDO to throw them?

    To make PDO throw exceptions you have to configure PDO errmode. Note that setting this mode as a connection option will let PDO throw exceptions on connection errors too, which is very important.
    So, here is an example for creating a PDO connection right way:

    $dsn = "mysql:host=$host;dbname=$db;charset=utf8";
    $opt = array(
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        // other options 
    );
    $pdo = new PDO($dsn, $user, $pass, $opt);
    

    Connecting this way, you will be always notified of all database errors, occurred during query execution. Note that you have to be able to see PHP errors in general. On a live site you have to peek into error logs, so, settings have to be

    error_reporting(E_ALL);
    ini_set('display_errors',0);
    ini_set('log_errors',1);
    

    while on a local development server it's ok to make errors on screen:

    error_reporting(E_ALL);
    ini_set('display_errors',1);
    

    and of course you should never ever use error suppression operator (@) in front of your PDO statements.

    Also, due to many bad examples telling you to wrap every PDO statement into try..catch block, I have to make a distinct note:

    DO NOT use try..catch operator just to echo an error message. Uncaught exception is already excellent for this purpose, as it will act just the same way as other PHP errors - so, you can define the behavior using site-wide settings - so, you will have your error message without this useless code. While unconditionally echoed error message may reveal some sensitive information to a potential attacker, yet confuse a honest visitor.

    • A custom exception handler could be added later, but not required. Especially for new users, it is recommended to use unhandled exceptions, as they are extremely informative, helpful and secure.
    • Use try..catch only if you are going to handle the error itself - say, to rollback a transaction.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?