Error establishing a database connection using Wordpress

mysql, php, wordpress

Solution

Try to change your test file to use the wp-config file.

     <?php
     //PATH TO YOUR FILE
     require_once('path/to/wp-config.php');
     //using wp-config variables 
     $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
     if (!$link) {
          die('Could not connect: ' . mysql_error());
     }
     echo 'Connected successfully';
     mysql_close($link);
     ?>

If this is working you dont have a code issue - you have a server/connectivity issue.

Problem

I have problem with my Wordpress site, it was working normally, Suddenly this message start to appear Error establishing a database connection Wordpress I tried the following things (no one is working and I still have the same problem till now) I checked my wp_config.php for db name and credential and every thing is fine and I tried to repair db by adding `WP_ALLOW_REPAIR` and this scripts shows that every thing is okay. I added test file to my website to see if the db credentials is correct like following ``` <?php $link = mysql_connect('localhost', 'myUserName', 'myPassword'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> ``` and it's print Connected successfully I tried to disable plugins by changing the name of the plugins folder, but same problem.

Original source