PDO 异常问题 - 如何捕捉它们

2021-12-26 00:00:00 sql exception-handling php pdo

我正在使用 PDO 为数据库重写网站界面.我曾经使用过mysql扩展,但我从来没有为错误处理而烦恼,而且我拥有的少数错误处理程序基本上都是复制粘贴的.

I'm using PDO to re-write a website interface for a database. I used to use the mysql extension, but I had never bothered with error handling, and the few error handlers I had were basically copy-paste.

现在我想正确地做这件事.但是,我在捕捉我想要的错误时遇到了问题(MySQL 中的重复条目"、空值"等错误).我的语句中有多少需要在 try 块中?所有的东西都应该在那里吗?我正在使用 Include() 连接到我的数据库(它有自己的错误处理),所以只有查询执行在此代码中有错误.我不明白为什么它在执行以下代码时没有捕获错误:

Now I'd like to do this right. However, I'm having issues catching the errors how I'd like (errors like "Duplicate Entry", "Null Value" etc in MySQL). How much of my statement needs to be in the try block? Should all of it be in there? I'm using an Include() to connect to my DB (which has its own error handling), so it's only the query execution which has errors in this code. I can't figure out why it's not catching an error when executing the following code:

try {
  $stmt = $db->prepare("INSERT INTO tbl_user (id, name, password, question, answer)    VALUES (NULL, :name, :password, :question, :answer)");
  $stmt->bindValue(":name", $_POST['name']);
  $stmt->bindValue(":password", $_POST['password']);
  $stmt->bindValue(":question", $_POST['question']);
  $stmt->bindValue(":answer", $_POST['answer']);
  $stmt->execute();
  echo "Successfully added the new user " . $_POST['name'];
} catch (PDOException $e) {
  echo "The user could not be added.<br>".$e->getMessage();
}

所以我的问题是:所有这些都必须在 try 块中吗?我可以将执行放在 try 块中吗?它应该捕获错误 Duplicate value "John" in key "name",而是通过成功消息.(尝试添加两个John"用户时).我检查了 PHPMyAdmin;索引是唯一的并且确实按预期抛出错误,只是不使用此代码.

So my questions: does ALL OF THAT have to be in the try block? Can I just put the execute in the try block? It should catch the error Duplicate value "John" in key "name", but instead goes through with the success message. (When trying to add two "John" users). I checked in PHPMyAdmin; the index is unique and does throw the error as expected, just not using this code.

推荐答案

您应该查看文档.但是如果你没有找到任何东西,你可以添加另一个捕获:

You should look at the documentation. But If you dont find anything, you can add another catch :

<?php
try {
  $stmt = $db->prepare("INSERT INTO tbl_user (id, name, password, question, answer)    VALUES (NULL, :name, :password, :question, :answer)");
  $stmt->bindValue(":name", $_POST['name']);
  $stmt->bindValue(":password", $_POST['password']);
  $stmt->bindValue(":question", $_POST['question']);
  $stmt->bindValue(":answer", $_POST['answer']);
  $stmt->execute();
  echo "Successfully added the new user " . $_POST['name'];
} catch (PDOException $e) {
  echo "DataBase Error: The user could not be added.<br>".$e->getMessage();
} catch (Exception $e) {
  echo "General Error: The user could not be added.<br>".$e->getMessage();
}
?>

这必须有效,因为 PHP 插件的所有异常都继承自 Exception 原生 PHP 类.(如果我记性好的话,从 5.0 开始).

This must work because all exceptions of PHP plugins herits from the Exception native PHP class. (Since 5.0 if my memory is well).

相关文章