我正在使用 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']);
    ...
}

关于SQLSTATE[08006] [7] 无法将主机名 "dbname="转换为地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38639867/

10-12 02:47