本文介绍了为什么这个PDO异常不被抓住?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个INSERT包装在一个try / catch,但是一个缺失的表没有被抓住,PHP在'$ dbh-> prepare'错误。
我设置了'PDO :: ATTR_ERRMODE',在'$ dbh-> prepare'为2之前,该值回传到浏览器。



如果表存在,则INSERT按照我的预期工作。我只发现有一个问题,当我故意放下桌子并运行代码时,测试时出现问题。



我忽略了什么?



提前感谢



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

  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有错误!; #尝试暂时向浏览器写入
错误(将页面加载信息写入数据库时​​出错$ e-> getMessage());
$ our-> db ['save'] = FALSE;
}
}


解决方案

你misspelt PDOException ;您有 PDOExeption (注意缺少的 c )。


I have an INSERT wrapped in a try/catch, but a missing table is not getting caught and PHP is erroring out at '$dbh->prepare'.I've set the 'PDO::ATTR_ERRMODE' and the value echoed to the browser just before the '$dbh->prepare' is 2.

If the table exists, the INSERT works as I expected. I only discovered there was an issue while testing when I purposely dropped the table and ran the code.

What have I overlooked?

Thanks in advance

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 no such table: submit_info' in C:\etc\httpd\htdocs\sqlite_data\gather.php:309

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;
    }
}
解决方案

You misspelt PDOException; you have PDOExeption (note the missing c).

这篇关于为什么这个PDO异常不被抓住?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:53