Jump to content

prevent "Can't connect" error message


smerny

Recommended Posts

i have ajax refreshing some content... and if my website goes down while someone is on it, they see this:

 

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

 

how do i prevent this?

Link to comment
Share on other sites

mysql_connect(blah blah blah) or die("Error Message")

Most people will argue that trigger_error() is the "proper way" to do it, but really, the proper way is to have a Database Class and have it do all that error checking stuff for you.

 

if you use the mysql_error() function .. (instead of "Error Message") it will give you the reason the connection failed, but for some reason I don't think you want that.

Link to comment
Share on other sites

Actually, because putting or anything() on the end of a statement doesn't address the execution path the remainder of your code takes when there is an error, it is only useful for troubleshooting problems and not ideal for application error handling.

 

On an error, your code should -

 

A) Output a user error message, something like "Due to a server error, this page cannot be accessed at this time."

 

B) Log all the available information about the error - who (ip address, logged in visitor id) caused the error (in case a hacker managed to cause the error or a legitimate visitor happened to do something that your logic could not handle), what the actual data values or query was that caused the error and what error resulted, when the error occurred (so that you could both see if someone is triggering a series of errors to bring your database server down or to match outages with actual problems with your database server), where in your code (file and line number) the error occurred.

 

C) Finish processing and output the remainder of your page that is not dependent on the error that did occur.

 

You should also have display_errors set to OFF and log_errors set to ON for a live server (error_reporting should always be E_ALL or higher) so that all php detected errors are not displayed but they are logged.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.