PDO Exception Questions - How to Catch Them
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();
}
?>
This must work because all exceptions of PHP plugins herits from the Exception native PHP class. (Since 5.0 if my memory is well).
Why can't I catch the error with PDOException?
The exception you are trying to catch will never be thrown, because you need to tell PDO how you want it to handle errors.
$con = new PDO($dsn,"root","xxxx");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Otherwise, the default PDO::ERRMODE_SILENT
will be used:
This is the default mode. PDO will simply set the error code for you to inspect using the PDO::errorCode() and PDO::errorInfo() methods on both the statement and database objects; if the error resulted from a call on a statement object, you would invoke the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object. If the error resulted from a call on the database object, you would invoke those methods on the database object instead.
Tangentially, you should be using prepared statements. You are using a prepare()
call, but you are not parametrizing the query and binding the variables as you should. Using quote()
is not secure enough.
2020 Update:
Interestingly, starting with PHP 8, the default behaviour for PDO will change and will throw exceptions by default. The change was voted on this RFC, which mentions:
The current default error mode for PDO is silent. This means that when an SQL error occurs, no errors or warnings may be emitted and no exceptions thrown unless the developer implements their own explicit error handling.
This causes issues for new developers because the only errors they often see from PDO code are knock-on errors such as “call to fetch() on non-object” - there's no indication that the SQL query (or other action) failed or why.
When PHP 8 is released on November 2020, the default error mode will be PDO::ERRMODE_EXCEPTION
.
PDO catch PDOException Error
It is a namespace issue.
When within a namespace, you have to address every root level class name by prepending it with a backslash.
But you didn't.
But the worst part is that you should not catch a PDOException at all. Just leave it alone and let it report to a site-wide report handler
catching a 'PDOException' when database is down
do a try/catch to catch exceptions
try{
$db = new PDO("mysql:host=".$host."; dbname=".$dbase."", $user, $pass); //line 16
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return $db;
} catch( PDOException $e){
$originalError = $e->getMessage();
echo 'something went wrong.. '.$originalError;
exit;
}
How to handle PDO exceptions
PDO won't throw exceptions unless you tell it to. Have you run:
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
on the PDO object?
pdo catch and output mysql errors
By default PDO is not in a state that will display errors. you need to provide the following in your DB connection
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
More info can be seen Here
Related Topics
Error 405 (Method Not Allowed) Laravel 5
Uncompress a Gzip File from Curl, on PHP
How to Get Array Key from Corresponding Array Value
Php: Date Larger Than Current Date
Utf-8 Special Characters Not Displaying
Escaping Column Names in Pdo Statements
PHP Directory List from Remote Server
PHP Datetime Createfromformat Functionality
The Requested Url /Projectname/Users Was Not Found on This Server. Laravel
Secure and Flexible Cross-Domain Sessions
How to? Form Post to Multiple Locations
Eloquent Many-To-Many-To-Many - How to Load Distant Relation Easily
How to Consume a Wcf Web Service That Uses Custom Username Validation with a PHP Page