April 2, 2014 / IST / How-to Guides, Web Development.

Error Establishing a Database Connection error occurs whether database is crashed or php memory limit by your hosting server.  Or this can happen for various reasons, but often users don’t know that their site is down. WordPress displays very ugly page.  In this article I show you  how to customize your database error page in WordPress. We will also show you how to setup a notification for every time your website goes down due to a database error.

error-establishing-a-database-connection

Open a new file and save it as “db-error.php”. Paste the following content inside that. Then upload the file in your /wp-content/ directory.

<?php 
/* Custome WordPress database error page */

//Set header
  header('HTTP/1.1 503 Service Temporarily Unavailable');
  header('Status: 503 Service Temporarily Unavailable');
  header('Retry-After: 600'); // 1 hour = 3600 seconds

?>

<!DOCTYPE HTML>
<html>
<head>
<title>Website is under maintenance.</title>
<style>
body {
	padding: 20px;
	background:#444;
	color: white; 
	font-size:30px;
	}
span{
	color:#CCC;
	font-size:20px;
}
</style>
</head>
<body>
	Sorry! Website is under maintenance. Try again after some time.
	<br/>
	<span>
	<?php 
		$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
		$web = $_SERVER['HTTP_HOST'];
		echo $protocol.'://'.$web.$_SERVER['REQUEST_URI'];
		  // Check if the "from" input field is filled out

		$from = "db-error@yoursite.com"; // sender
		$subject = "Database error found! ".$web;
		$message = $web." is not opening. Please check as soon as possible.";
		// send mail
		mail("user@website.com",$subject,$message,"From: $from\n");

	?>
	</span>
</body>
</html>

 For mail notification:

Look bottom of this code. In mail function replace user@website.com to your email. After uploading “db-error.php” you will get db error email at every “Error Establishing a Database Connection“.

How to check.

Open wp-config.php on editor from root folder. Set wrong db or user name. (For only test). Open website in any browser. You will see new db error page and check e-mail too.

After testing set right  db or user name.