PDO异常问题 - 如何抓住他们 [英] PDO Exception Questions - How to Catch Them

查看:146
本文介绍了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块中吗?它应该捕获错误键名称中的重复值John,而是成功显示消息。 (当尝试添加两个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.

推荐答案

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

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).

这篇关于PDO异常问题 - 如何抓住他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆