問題描述
我正在使用 PDO 為數(shù)據(jù)庫重寫網(wǎng)站界面.我曾經(jīng)使用過mysql擴展,但我從來沒有為錯誤處理而煩惱,而且我擁有的少數(shù)錯誤處理程序基本上都是復(fù)制粘貼的.
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.
現(xiàn)在我想正確地做這件事.但是,我在捕捉我想要的錯誤時遇到了問題(MySQL 中的重復(fù)條目"、空值"等錯誤).我的語句中有多少需要在 try 塊中?所有的東西都應(yīng)該在那里嗎?我正在使用 Include()
連接到我的數(shù)據(jù)庫(它有自己的錯誤處理),所以只有查詢執(zhí)行在此代碼中有錯誤.我不明白為什么它在執(zhí)行以下代碼時沒有捕獲錯誤:
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 塊中嗎?我可以將執(zhí)行放在 try 塊中嗎?它應(yīng)該捕獲錯誤 Duplicate value "John" in key "name"
,而是通過成功消息.(嘗試添加兩個John"用戶時).我檢查了 PHPMyAdmin;索引是唯一的并且確實按預(yù)期拋出錯誤,只是不使用此代碼.
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.
推薦答案
您應(yīng)該查看文檔.但是如果你沒有找到任何東西,你可以添加另一個捕獲:
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).
這篇關(guān)于PDO 異常問題 - 如何捕捉它們的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!