【问题标题】:Why isn't this PDO Exception getting caught? [closed]为什么这个 PDO 异常没有被捕获? [关闭]
【发布时间】:2012-03-03 12:09:46
【问题描述】:

我有一个包含在 try/catch 中的 INSERT,但是没有捕获到丢失的表,并且 PHP 在 '$dbh->prepare' 处出错。 我已经设置了 'PDO::ATTR_ERRMODE' 并且值在 '$dbh->prepare' 为 2 之前回显到浏览器。

如果表存在,则 INSERT 会按预期工作。我只是在测试时故意放弃表并运行代码时才发现存在问题。

我忽略了什么?

提前致谢

PHP 致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[HY000]:一般错误:1 没有这样的表:C:\etc\httpd\htdocs\sqlite_data\gather.php:309 中的 submit_info'

if($our->db['save']) {
    try {
        echo $dbh->getAttribute(constant('PDO::ATTR_ERRMODE'));
        $sth = $dbh->prepare(
            "INSERT INTO submit_info( post_time, post_completed, post_size , script_name, user_agent  )" .
            " VALUES ( datetime(:request_time, 'unixepoch'), datetime(:current_time, 'unixepoch'), :content_length, :script_filename, :user_agent );"
            );
        $sth->bindValue(':request_time', (@$_SERVER['REQUEST_TIME'] + 0), PDO::PARAM_INT);
        $sth->bindValue(':current_time', time(), PDO::PARAM_INT);
        $sth->bindValue(':content_length', (@$_SERVER['CONTENT_LENGTH'] + 0), PDO::PARAM_INT);
        $sth->bindValue(':script_filename', @$_SERVER['SCRIPT_FILENAME'], PDO::PARAM_STR);
        $sth->bindValue(':user_agent', (@$_SERVER['HTTP_USER_AGENT'] . ''), PDO::PARAM_STR);
        $sth->execute();
        $our->db['submit_id'] = $dbh->lastInsertId();
    } catch (PDOExeption $e) {
        echo "There was an error!"; # try writing something to the browser temporarily
        errors("Error writing page load information to database: " . $e->getMessage());
        $our->db['save'] = FALSE;
    }
}

【问题讨论】:

    标签: php exception-handling pdo


    【解决方案1】:

    你拼错了PDOException;你有PDOExeption(注意缺少的c)。

    【讨论】:

    • 这确实是问题所在。这是我从事的第一个 PDO 项目。由于那封丢失的信,我花了 2 个小时查看文档和逻辑。 Komodo 通常会标记拼写错误,或者我在日志中收到运行时错误,指出我的方向是正确的。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2019-10-02
    • 2012-01-01
    • 1970-01-01
    • 2011-04-12
    • 2010-11-16
    • 2012-01-22
    相关资源
    最近更新 更多