Error in connection with bank using PDO

Asked

Viewed 5,829 times

3

Error message:

Error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: This host is not known.

code

    $db_name = 'db_name';
    $hostname = 'mysql.hostinger.com.br';
    $username = 'username';
    $password = 'password';

    $sql = "INSERT INTO atividade(titulo, descricao, data_atividade) VALUES(:titulo, :descricao, :data_atividade);";

    try {
        $dbh = new PDO('mysql:host=$hostname;dbname=$db_name', $username, $password);
    } catch (PDOException $e) {
        echo 'Error: ' . $e->getMessage();
    }`

I just left the $hostname filled out correctly because it’s him who’s making the mistake. If anyone can help me, I’d appreciate it!

  • Hostinger in free version does not make connections to mysql database; for this you have to buy a hosting package

  • In the Hostinger Cpanel go to ''Mysql Remote '' and enable the database, simple.

2 answers

5


Basically this domain name is not accessible on the public network. If the code is hosted on Hostinger, just put it as localhost. Another alternative is to put the ip address of the mysql server (which is the same ip as the hosting).

2

Actually the hostname is incorrect, to access via external php (outside of the Hostinger localhost) or any other language, you must use 'sql141.main-hosting.eu'

$db_host = "sql141.main-hosting.eu";
$db_user = "u265779999_meme";
$db_pass = "admin";
$db_name = "u265779999_teste";
$conexao = mysqli_connect("$db_host", "$db_user", "$db_pass", "$db_name");

if(mysqli_connect_errno($conexao)){
    $resultado = mysqli_connect_error();
    exit;
}

Browser other questions tagged

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