1

I know this same warning has been asked many times here. But in all the queries I saw some mistake in the program because of which the user was not able to access the database.

But in my case I am accessing directly using the user which has access rights to the database, but even then this warning is coming

php_network_getaddresses: getaddrinfo failed: No such host is known

<?php

$conn_error = 'Could not connect.';
$mysql_host  = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';

$mysql_db = 'a_database';

if(!mysqli_connect('mysql_host','mysql_user','mysql_pass','mysql_db')){
die($conn_error);
}
else{
  echo 'connected';
}

It is giving the error message of not connected.

Although the users have the privilege to access the database. enter image description here

1
  • 1
    you really should learn to read the error message. "no such host" means that your code can't even TRY to log in to mysql, since it can't even reach the db server.
    – Marc B
    Commented May 22, 2015 at 18:52

1 Answer 1

5

You forgot to put the $ in (as variables)

if(!mysqli_connect('mysql_host','mysql_user','mysql_pass','mysql_db'))

those are being treated as strings.

  • You're trying to connect to a host called "mysql_host", which is why it's not finding the intended "localhost" server address, as per $mysql_host = 'localhost';.

Therefore, replace that with; while removing the quotes.

Sidenote: If you intend on putting the $ inside single quotes, it won't work. Variables do not get parsed in single quotes, but in double quotes, however they're not required here. Consult "references" below.

if(!mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db))

and this die($conn_error); (Could not connect.) doesn't help you.

This does:


Reference(s):

0