Macbernie Macbernie - 4 months ago 21
SQL Question

SQLSTATE[08006] [7] could not translate host name "dbname=" to address

I'm using the Symfony/Silex SecurityServiceProvider to login users on the site.

http://silex.sensiolabs.org/doc/providers/security.html


I follow the guide step by step, but when I submit the login form, I got a PostgreSQL error

SQLSTATE[08006] [7] could not translate host name "dbname=" to address


Whatever I set for dbname, I still got the error. Even with the right IP of the distant server. However my setup is good, because I use the same class to connect and request the server, and it always work.

The error appears only on submitting the login form !

My login class

<?php

namespace Model;

use Silex\Application;

abstract class Entity {

private $app;
private $host;
private $base;
private $port;
private $user;
private $pass;
private $charset;

public function __construct(Application $app)
{
$this->app = $app;

$this->host = $app['config']['database']['host'];
$this->base = $app['config']['database']['base'];
$this->port = $app['config']['database']['port'];
$this->user = $app['config']['database']['user'];
$this->pass = $app['config']['database']['pass'];
$this->charset = $app['config']['database']['charset'];
}

protected function connectBDD()
{
$connect = new \PDO("pgsql:host=$this->host;dbname=$this->base", $this->user, $this->pass);
$connect->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$connect->query("SET NAMES '$this->charset'");

return $connect;
}
}
}


Thanks for help

Answer

You config values are empty. $app['config']['database']['host'] is empty and connection looks like $connect = new \PDO("pgsql:host=;dbname=", ...);
Try to dump config to see values

public function __construct(Application $app)
{
    dump($app['config']['database']);
    ...
}
Comments