Error when establishing a connection with the Database

0

I'm getting this message several times:

Error establishing a connection to the Database

IhavereadsomematerialaboutthisproblemandalwaysthesolutionistoverifytheWP-configfileandverifythattheMYsqlusernameandpasswordarecorrectandcorrect.

IsthereanyotherwaytocheckwhatmaybeoverloadingMysql?

Inmythemeintheindex.phpfile,Iaddedthecodebelow:

<?phpechocatch_that_image(25,14)?>

andintheotherfunction.phpfile:

functioncatch_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 displaying them in the theme.

Is this code related to the Mysql error?

    
asked by anonymous 17.01.2016 / 15:13

1 answer

2

This is a very simple problem to solve.

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

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

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

Detail: charset is utf8 same, no hyphen.

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

    
18.01.2016 / 02:12