As a website owner, you want your WordPress website to have no downtime and load immediately. But sometimes when you type in the website’s URL, you get nothing but a blank page. That’s when you realize there is an error establishing a database connection in WordPress.
Unfortunately, this is how you can lose users. Some of them may never return with the assumption that the website is no longer functional. That’s a big price to pay for a database connection error.
A website cannot afford downtime. In today’s world, people’s attention span is limited to a few seconds. And a website is expected to load in less than three seconds. Imagine the plight when your website throws up a blank screen to your users.
In this article, we shall go over the reasons and resolutions for a black screen. There are ways to troubleshoot such errors and you can fix the problem yourself.
When a website is about to load, the PHP code that is used to build the website accesses the database, which is written with MySQL and pulls out all the information needed to create the page that is about to load.
Every button you click on the website, a PHP query is sent to access the data connected to it so the page loads in time for the user.
Now, when you see a blank page or the message “Error establishing database connection”, it means that the PHP code wasn’t able to make a connection with the database to retrieve the information it needs to display the webpage. The black page or the error message isn’t enough to determine what went wrong.
There are several reasons why there would have been an error establishing a database connection on your website like:
Database is Corrupted – this can happen when the plugins or themes in your WordPress website aren’t upgraded. A bad code might lead to a corrupt database.
Server Downtime – your web-host may have a server downtime issue that would result in your website being down. It may also happen if there’s a massive spike in your website’s traffic.
Log in Error – if your database and website’s login credentials are different and have been recently changed, then your website may not be able to access the database.
How to Fix the Error?
First off, log into your hosting account and download all the web files and export the database to create a backup.
Now find the cause of the error.
1. Corrupted Database
Are you faced with the same error as you enter the website URL from the front-end as well as when you log in from the back-end (/wp-admin/)?
Does it show you the message that the database is not available or that it needs repair? This means, your database is corrupted and needs to be repaired.
Thankfully, WordPress has a built-in database repair code. You can access this code in your cPanel and in the File Manager.
Now select the folder you installed WordPress in. This is your wp-config. Add the line “define( ‘WP_ALLOW_REPAIR’ , true ); to the code at the bottom.
This line will ensure your database will repair itself when you log into yourwebsite.com/admin/maint/repair.php. You will then get two options – to repair, or to repair and optimize. Click on either one of them, though repair, along with optimizing will take longer.
Now that your database is repaired, you must delete the line of code you added to wp-config. This is because the repair page is not as secure as you want it to be.
Anybody can access your URL without having to log in. Nothing to worry about if you go back and delete the code.
2. Login Credentials
If this didn’t work, check the login credentials of your database. As mentioned earlier, if your database and your website’s login credentials are different, your PHP code might not be able to access the MySQL database.
Go to your wp-config and open it with a text editor. Right on top, you will see the database details with the credentials.
Make the changes to the values in the second set to update your credentials to the correct ones. This should fix the problem immediately.
3. Corrupt Files
Sometimes the core files of your WordPress website may be corrupted because of a badly written code or by any other means. By re-uploading all your core files, you can fix this problem. It’s almost like it resets everything for you.
But steer clear from your PHP file, which has all the important setup data and your wp-config, which has all the themes and plugins installed in it.
If you re-upload these, you may lose a lot of the original data. And don’t forget to backup all your data before you re-upload it.
4. Outdated Plugins and Themes
It’s tricky with plugins and themes. When you get the one you need, you immediately install them. Many plugins are redundant and when not updated in over a year, dormant.
If you think your plugin is the problem, uninstall it, re-install it, and activate it again. This should help with connecting to your database again.
5. Check with the Hosting Company
If you still face the same error, reach out to your hosting company. Many times there are errors because of their server issues.
Give them all the details of the errors you find and how you tried to fix them. Once they know the problem, they will be able to help.
We may not know all the reasons why you might get an error establishing database connection with WordPress, but by checking every outlet, you will be able to find the root cause of the problem.
If you can fix the problem by yourself, it’s great. But if you can’t, don’t pick at it over and again. Simply contact a developer to help you with it.
And if they can’t help, reach out to your hosting company. If they can’t resolve it, then you know that the error is not a server issue.