In the official osCommerce Forum I got into bit of a conversation with a Store Owner, part of the conversation was all about what happens if your MySQL server goes down whilst the webserver is still up and running.
For those who don’t know, MySQL runs on it’s own little server as part of the overall web server. So it is possible for one to be down whilst the other is functioning OK…
OK, so, in a standard osCommerce shop, what will happen is that a message will be displayed, like this:
Unable to connect to database server!
Which isn’t so great for anything – after all, it is your CUSTOMERS who will see this! So, my idea was to make this message a little better for you and your customers.
Open up the file /includes/application_top.php and find
tep_db_connect() or die(‘Unable to connect to database server!’);
This line of code is simply saying “Connect to the Database OR kill the connection and show the “unable to…” message. We are going to change this line of code to;
@tep_db_connect() or header(“Location: busted.html”);
Now we are saying “Connect to the Database OR redirect to a page called busted.html”. Easy, eh? Notice that we also put an “at” (@) sign in front of tep_db_connect – this is important as we wish to make sure we suppress any other error messages.
Save the /includes/application_top.php file and upload it to your server to replace the old one (which you might like to backup first!).
The next step is to make a new HTML page called “busted.html”. On this page you can have anything you like – maybe an error message, maybe a “contact us by phone” message – maybe something else entirely!
However, there is one VERY IMPORTANT CAVEAT – the “busted.html” page MUST NOT connect to your database! As your database server is down, it would be completely pointless to do so.
Of course, the page can be .php if you wish and it doesn’t have to be called “busted.html” – it can be called anything you like. Just make sure that the name fo the file and the “Location” line of code match exactly!
You could send yourself an error message by email, or you could even send a signal to the server to restart the SQL server (this would be for very advanced users who have control of their own server). For most of us, a simple HTML page will suffice.
Why 404 redirect won’t work in this case
If your SQL server is down, pages are still presented by the webserver, so they do not 404 (error out) and redirect. What this means is that you could go to; site.com/product_info.php?products_id=123 and see “Unable to Connect” – this is NOT a 404 and so it won’t redirect to an error page. Does this make sense?