Error establishing a connection to the database

I am getting this message several times:

Error establishing a connection to database

insert the description of the image here

I have read some material about this problem and always the solution they indicate is to check the WP-config file and check if the MySQL username and password are correct, and they are correct.

Is there any other way to check What Can is overloading Mysql?

In my theme in the index file.php, I added the code below:

   <?php echo catch_that_image(25, 14) ?>

And in the function file.php this other:

function catch_that_image($w, $h) {
global $post, $posts;
$first_img = '';
$new_img_tag = "";

ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

if(empty($first_img)){ //Defines a default image with 0 width
    $new_img_tag = "<img src='/images/noimage.jpg' width='0px' class='' />";
}

else{
    $new_img_tag = '<img alt=" - ' . $post->post_title . ' - " title="' . $post->post_title . '" src="' . $first_img . '" width="' . $w . '" height="' . $h . '" class="" />';
}

return $new_img_tag;
}

This code is for calling images hosted outside of wordpress and showing them in the theme.

Can this code be related to MySQL error?

Author: Endou, 2016-01-17

1 answers

This is a very simple problem to solve.

Is giving error, but why? Of course, there is a problem with the connection, so at first it can be the login data or the access permission.

Is the bank located on the same server where the WP files are? If it is on another server, remember that it is necessary to release remote access and in the firewall (if any).

Create a file .php with the code below and change the connection information by informing the correct data.

<?php

$host = ''; //Se o banco de dados estiver no mesmo servidor deste arquivo, use localhost
$database = '';
$user = '';
$password = '';
$charset = 'utf8';

header('Content-type: text/html; charset=utf-8');

try{
    $pdo = new PDO("mysql:host={$host};dbname={$database};charset={$charset}",
        $user, $password);
    echo 'Conexão feita com sucesso.';
} catch (PDOException $e) {
    echo '<h1>Falha na conexão com o banco de dados</h1>'
        .'<hr>'
        .'<pre>';
    var_dump($e);
}

If it gives error, it will display the reason for the error, just read the output that will be printed on the screen.

Detail: the charset is utf8 same, without hyphen.

If the data is correct, the access permissions are correct, and there are no errors in the connection code, then what remains is: MySQL is not running or is misconfigured.

 2
Author: Clayderson Ferreira, 2016-01-18 01:12:05