Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Error logging and handling - best practice

I'm building a site using PHP which allows users to add in a lot of data to various tables in a MYSQL database. This requires a lot of inserting, updating and dropping of tables in my datatbase, sometimes running several commands in one script.

I'm concerned about catching potential errors occurring when live (I've tested, tested and tested but still want a back up plan).

I've searched everywhere for a solution but cannot find one that would satisfy my needs so wondered if anyone here had any practices they use or advice they can give me.

What I want to happen:

If an error occurs connecting to my database (for instance) I want to display a page or alert window with a "sorry we've had a problem" message with a button to log the error. When a user clicks the button I want to be able to log a mysql_error() to the database with a description of the failed command/function and page name along with time/date stamp which I can track.

Is this something anyone has done before or can offer an alternative? Or is there a built in function that does exactly this which I have missed?

Any advice would be much appreciated.

like image 241
Paul Avatar asked May 14 '12 19:05

Paul


1 Answers

If you fail connecting to the DB, you won't be able to log the error to the db. The bad connection scenario aside, you should use a php mysql library that supports exceptions (like PDO) and use try-catch blocks to catch error states you want to log.

You'll probably want to just write to the apache error log on DB connection failure (can be done in a try-catch block).

like image 98
Ray Avatar answered Oct 19 '22 11:10

Ray