Eric Sietsema
Eric Sietsema

Reputation: 31

PDO error handling and storing errors in database

How would I go about getting PDO statements to generate a safe error message? I don't want the user to see the error message. I want them to get directed to a page that says a clean message, "Whoops something unexpected happened!". I would also like to log the errors in a database to review and catch errors others are generating.

I'm using PHP and MySQL.

I found that when you make your connection you can set your error handling like this.

$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Anyone do anything like this before?

Upvotes: 2

Views: 1157

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157892

You have to understand that PDO do not generate a "safe" or "unsafe" error message. It does generate an error message. That's all. The rest is is the responsibility of site-wide PHP settings.

PDO is not the only source of errors. Why care of PDO errors only? Why not to handle ALL errors the same way?

Want errors logged? It's a matter of one PHP ini setting. Want errors not to be displayed? It's a matter of one PHP ini setting. Want generic error page to be shown? It's a matter of simple function that will handle all errors at once.

Everything can be done proper and straight way, without wrapping every statement into try catch. Without writing into log manually. Without even single additional line of code.

You need to set up PHP error handling, not PDO.

And of course, it makes absolutely no sense in trying to store a database error in the same database that failed you right now. Errors have to go into error log on a live server and on screen - on a local development PC.

Anyone do anything like this before?

Sure. Every single one of 1000000s sites in the world. The way described above.

Upvotes: 1

Adam Brown
Adam Brown

Reputation: 2870

So this is just a suggestion as I have never tried this but after thinking about it a bit I think it would be an interesting option to explore. As I am fairly new to PHP & PDO I'm sure there are other and better ways.

Perhaps you could try using the try function of PHP and then instead of echo'ing (if failed) the PDOException you could run another function that prints it to a text file. Something like.

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    foreach($dbh->query('SELECT * from FOO') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    $strFileName = 'whatever.txt';

    if(!is_writable($strFileName))
        die('Change permisions to ' . $strFileName);

    $handle = fopen($strFileName, 'a+');

    fwrite($handle, "\r" . $e->getMessage() . "\r");
    fclose($handle);
}
?> 

This way you would avoid a DB connection (which is the problem I guess) but still save the error. You would perhaps want to omit the echo'd text after die within the if statement.

Upvotes: 2

Patrick Kostjens
Patrick Kostjens

Reputation: 5105

I think it is better to write your logs to a file, instead of a database. Especially since you want to log PDO errors, which indicate something is wrong with your database connection.

You can show the user a nice error page by catching your errors. You can redirect your users to your error page then, in case something went wrong.

Upvotes: 1

Related Questions