5

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.

1
  • You're going to have to try/catch the mysql errors. Generic PHP error logging is usually supported by most web-servers. It simply generates a log file of all PHP errors that get raised. Commented May 14, 2012 at 19:30

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.