WordPress Errors 10 min read

How to Fix WordPress "Error Establishing a Database Connection": Complete Guide

Advertisement

Google AdSense In-Article Display Placement

Publisher ID: ca-pub-XXXXXXXXXXXXXXXX (Configured in config.php)

This classic WordPress error occurs when WordPress PHP files cannot authenticate or exchange data with the MySQL/MariaDB database server. The issue typically involves incorrect credentials in wp-config.php or corrupted database tables.

✔ Rapid Direct Solution

Open your "wp-config.php" file and verify the four core constants: DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. Test these credentials using a small standalone test script. If credentials are correct, check whether visiting "/wp-admin" displays "One or more database tables are unavailable", and add "define('WP_ALLOW_REPAIR', true);" to repair them.

1. Problem Diagnosis & Symptoms

Every dynamic page load in WordPress queries the MySQL database for posts and theme options. When mysqli_connect fails inside the wpdb class, WordPress immediately aborts rendering.

2. Primary Root Causes

  • Cause: Incorrect database password in wp-config.php.
  • Cause: Corrupted database tables.

3. Step-by-Step Diagnostic Fixes

1

Inspect wp-config.php Database Constants

Check DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.

PHP
define( 'DB_NAME', 'wp_db' );
define( 'DB_USER', 'wp_user' );
define( 'DB_PASSWORD', 'Password123' );
define( 'DB_HOST', 'localhost' );

4. How to Verify the Resolution

  • ✔ Load WordPress homepage and WP-Admin.

5. Diagnostic Troubleshooting Matrix

Symptom Observed Root Cause Recommended Action
wp-admin says: One or more tables are unavailable Database tables corrupted Add WP_ALLOW_REPAIR to wp-config.php and run repair.php

6. Critical Mistakes to Avoid

  • ❌ Never leave WP_ALLOW_REPAIR permanently enabled.

7. Frequently Asked Questions

❓ What should DB_HOST be in Hostinger?

On Hostinger, DB_HOST is "localhost".

Official Technical References

Advertisement

Google AdSense In-Article Matched Content Unit