Able to use all admin pages but in the frontend there is a “Error establishing a database connection”-Error

Make sure your database connection is correct

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

And you should test with plain php file for connecting database like below codes

<?php

$connect = mysql_connect('localhost', 'root', 'password');
if (!$connect) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($connect);

To make it flexible on localhost or production, use this way,

<?php

switch ( $_SERVER['HTTP_HOST'] ) {
    case 'yourdomain.dex': //local
        define( 'DB_NAME', 'rino' );
        define( 'DB_USER', 'root' );
        define( 'DB_PASSWORD', '' );
        define( 'DB_HOST', 'localhost' );
        define( 'WP_DEBUG', true );
        define( 'WP_DEBUG_DISPLAY', true );
        define( 'SCRIPT_DEBUG', true );
        define( 'WP_DEBUG_LOG', true );
        define( 'FS_METHOD', 'direct');
        @ini_set( 'display_errors', true );
        define( 'ENV', 'dev' );
        break;

    case 'yourdomain.staging.com': //staging
        define( 'DB_NAME', 'creati_444' );
        define( 'DB_USER', 'creatisd' );
        define( 'DB_PASSWORD', 'lPC44@8u1B' );
        define( 'DB_HOST', 'localhost' );
        define( 'WP_DEBUG', true );
        define( 'WP_DEBUG_DISPLAY', true );
        define( 'SCRIPT_DEBUG', true );
        define( 'WP_DEBUG_LOG', true );
        @ini_set( 'display_errors', true );
        define( 'ENV', 'staging' );
        break;

    default : //production
        define( 'DB_NAME', 'keikfi' );
        define( 'DB_USER', 'siekfi' );
        define( 'DB_PASSWORD', 'lPC@8#u1B' );
        define( 'DB_HOST', 'localhost' );
        define( 'WP_DEBUG', false );
        define( 'ENV', 'pro' );
        break;
}


$protocol = ( ! empty( $_SERVER['HTTPS'] ) ) ? 'https://' : 'http://';

define( 'WP_SITEURL', $protocol . $_SERVER['HTTP_HOST'] );
define( 'WP_HOME', $protocol . $_SERVER['HTTP_HOST'] );

With above codes, you don’t need to care local or production, it would work because we make website domain change on the fly. Right?

And also, you should test with table repair

define('WP_ALLOW_REPAIR', true);

The above solutions doesn’t work, it is time to talk with host provider 🙂