I have made another connection before the same with another database, when changing the database it gives me this error and I do not see the error anywhere:
ERROR: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for localhost failed: Unknown host
This is the content of config.php
:
$nombre_servidor = "localhost";
$nombre_usuario = "root";
$password = "";
$nombre_base_datos = "usu";
This is the connection:
<?php
private static $conexion;
public static function abrir_conexion(){
if(!isset(self::$conexion)){
try{
include "config.inc.php";
print "hola";
self::$conexion = new PDO("mysql:host= $nombre_servidor; dbname=$nombre_base_datos; port=3307; charset=utf8", $nombre_usuario, $password);
self::$conexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Si ponemos lo siguiente comentado no funciona
// self::$conexion -> exec("SET CHARACTER utf8");
}catch(PDOException $ex){
print "<db>"."ERROR: " . $ex -> getMessage() . "<br>";
die();
}
}
}
When I call to open connection, it writes the hello to me, but then I get the error message.
Here Localhost:
Here the error:
You have a problem in the following line:
The hostname should come immediately after the symbol
=
, but you have left a blank space that is being included in the hostname that is trying to be resolved by DNS.You need to remove that whitespace (just like you do with
dbname=
) for the problem to be fixed:If you see the source code of the page that generated the error, you will notice that there are two blank spaces in
getaddrinfo for localhost failed
(betweenfor
andlocalhost
), but when you see the result in the web browser, it merges both blank spaces into a single one.The error messages in different versions of PHP are (they all start in common with
ERROR: SQLSTATE[HY000] [2005]
):8.1
8.0, 7.4 and 5.6
5.4