【发布时间】: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