How many times have you seen something like this at the top of a PHP file?
if (!($dbconnection = @mysql_connect(‘localhost’, ‘user’, ‘pwd’))) {
die ‘Could not connect to the database.’;
It’s so simple that it went to many scripts running on the Web. Mine were not exception. I never thought that it can hit me, but one day, after checking what kind of information Google has on Kyusl.com, I was shocked to see that message about the database problem in the Google cache. And that for the home page! I could never imagine that Google bot will reindex my site exactly at the same moment when the database is down or overloaded!
Getting Google site entry in a bad shape takes some time to recover. To have some protection for future problems, the first thing I did was to examine all PHP-based pages to decide what can run without the database and what needs it. If the database is not available, I can still display the home page, “about” and several other pages. Normally they check for an existing user session, using the database, but I adjusted them so that if there is no database connection, it is assumed that the user is not logged in, and general information is displayed.
The trick is very simple. At the top of each script for which the database is not vital I set a flag telling that in case of database problems, the script should not be stopped:
if (!($dbconnection = @mysql_connect(‘localhost’, ‘user’, ‘pwd))) {
if( !isset($no_db_error) ) {
print ‘<html><h3>Sorry, I could not connect to the database. Please try again later.</h3></html>’;
exit;
I hope that the database problems will not occur frequently, but even if they do, now Kyusl.com will show a better face in such cases.