问题:SQLSTATE[08006] [7] 无法将主机名“dbnameu003d”转换为地址

我正在使用 Symfony/Silex SecurityServiceProvider 登录网站上的用户。

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

我一步一步按照指南进行操作,但是当我提交登录表单时,出现 PostgreSQL 错误

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

无论我为 dbname 设置什么,我仍然得到错误。即使使用远程服务器的正确 IP。但是我的设置很好,因为我使用同一个类来连接和请求服务器,并且它总是有效的。

该错误仅在提交登录表单时出现!

我的登录类

<?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;
    }
}
}

感谢帮助

解答

您的配置值为空。$app['config']['database']['host']为空,连接看起来像$connect = new \PDO("pgsql:host=;dbname=", ...);

尝试转储配置以查看值

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

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐