How ugly is that message – Error Establishing Database Connection? I know, it does not sound good and so to the search engines and your website visitors. If you are using WordPress, then you might have come across this error message quite a number of times. The error generally means that the WordPress is not able to connect to the database due to several reasons – unable to reach database server or invalid database/user/password. And the most scary thing is, it gives a bad impression to your customers and plays a negative SEO. And another thing is, you (the website owner/admin) are not aware of the issue until someone says “hey! your website is down” – Ahh! that hurts isn’t?
So the ideal way is to tell your website visitors (with a custom (understandable) message) and search engines (by setting HTTP Error 503 Service unavailable), that the issue is temporary and you’re already working on it. Also, it’s a good idea to let the website administrator know about the issue via mail. Now, this sounds a lot better isn’t?
Between, what’s 503 Error status?
In simple terms, it means something is wrong and it’s a temporary condition – which will just vanish after some delay.
How to display a Custom Message or replace “Error Establishing Database Connection”
Step 1: Create a file called db-error.php in wp-content folder.
Step 2: Copy and paste the below code in db-error.php file
<?php // Tell Search Engine $protocol = $_SERVER["SERVER_PROTOCOL"]; if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) $protocol = 'HTTP/1.0'; header( "$protocol 503 Service Unavailable", true, 503 ); header( 'Content-Type: text/html; charset=utf-8' ); header( 'Retry-After: 600' ); // Get Variables $temp = mysql_error(); $url = curPageURL(); // Get Page URL function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } // Send The Alert mail( 'your@mail.com', 'SQL Error on my Website', 'This is an automated message, there is a SQL error, here is the message : '.$temp.'. Current URL is : '.$url); ?> <html> <head> <title>The website is down</title> <style> body { background: #FFFFFF ;font: 16px verdana, serif; line-height: 1.3; margin:0; padding:0; } #wrapper { height: 225px; margin: 80px auto 0; width:575px; } h1 { font-size: 34px; font-weight: normal; margin-top: 0; } p { margin: 0 0 10px 5px; } </style> </head> <body> <div id="wrapper"> <h1>Something went wrong and we are working on it!</h1> <p>An alert has been sent to the webmaster, please try again in 10 minutes.</p> </div> </body> </html>
Above code credits: remicorson
Brief explanation about the code: (Advantages of displaying custom message for Database error)
- Let Search engines know that the issue is temporary – To do that, we have to set header with HTTP 503 Service unavailable status.
- Display custom message to the visitor – you need to change the content of ‘wrapper‘ div.
- Send an alert mail to the website administrator – Change the email-id in the ‘mail‘ function.
Step 3: Test it!
Warning: You can test if this custom message is displaying properly by reproducing “Error Establishing Database Connection” error – by changing the database name or username in wp-config.php. But remember, your website will go down immediately. DO NOT test this on a production machine.
Ok! Now go ahead and fix “Error Establishing Database Connection” at the backend. Check if wp-config.php file holds the correct values:
define('DB_NAME', 'databasename'); define('DB_USER', 'databaseuser'); define('DB_PASSWORD', 'databasepwd'); define('DB_HOST', 'databaseserver');
That’s it! and Here’s the Bonus…