Error when establishing a connection to the Database

Asked

Viewed 2,569 times

0

I’m getting this message over and over again :

Error when establishing a connection to the Database

inserir a descrição da imagem aqui

I read some articles about this problem and always the solution that indicate is to check the WP-config file and check if the Mysql username and password are correct, and are correct.

Is there any other way to check what might be overloading Mysql?

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

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

and in the Function.php file 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 to call images hosted outside wordpress and shows them in the theme.

This code may be related to Mysql error?

  • Make sure the mysql user and password are correct.

  • I’ve checked and they’re correct

  • You could paste the error and the connection code into the post?

  • I edited the question look there

  • I attached an image with error

  • Check that the Mysql server is active and connected, for example using service mysqld status, if you have access to the SSH of the Mysql server or use mysql -h 111.222.333.444 -p -u Usuario DB

  • are staying in the same accommodation?

Show 2 more comments

1 answer

2

This is a very simple problem to solve.

It’s a mistake, but why? Of course there’s a problem with the connection, so at first it could be the login data or the access permission.

Is the bank located on the same server where the WP files are? If you’re on another server, remember that you need to free up remote access and firewall access (if any).

Create a. php file with the code below and change the connection information by stating 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 error occurs, it will display the reason for the error, just read the output that will be printed on the screen.

Detail: the charset is utf8 even without hyphenation.

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 not configured correctly.

  • 1

    If this solves the problem, I suggest renaming the question to "How to debug a Mysql connection"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.