捕获sql错误

时间:2015-03-26 09:38:55

标签: php sql pdo error-handling

我有一种更新数据库记录的方法,但我忘了绑定一个值(:date)。当这个方法调用它时,返回正面语句,而不是错误。如何捕捉到这样的错误? 附:抱歉英文不好

public static function update($id, $header, $body, $hidden, $date, $category){
    $db = db::connect();
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        $stmt = $db->prepare("UPDATE articles
                            SET Header = :header, Body = :body, Date= :date, Hidden = :hidden, ArtCatId = :category
                            WHERE _id = :id");
        $stmt->bindParam(':id', $id);
        $stmt->bindParam(':header', $header);
        $stmt->bindParam(':body', $body);
        $stmt->bindParam(':date', $date); //this one i've forgot
        $stmt->bindParam(':hidden', $hidden);
        $stmt->bindParam(':category', $category);
        $stmt->execute();      
    } 
    catch (PDOException $exc) {
        return $exc->getMessage();
    }
    return 'Article updated';

0 个答案:

没有答案