错误 SQLSTATE[08006] [7] 无法将主机名“port=5432”转换为地址:名称或服务未知

问题描述 投票:0回答:0

我需要重做从Mysql到Postgres的数据库连接。使用以下函数连接到 Mysql:

 public function getConnection()
    {
        $this->conn = null;

        try {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->exec("set names utf8");
        } catch (PDOException $exception) {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}

我尝试重做该功能:

public function getConnection()
    {
        $this->conn = null;

        try {
            $this->conn = new PDO("pgsql:host=$host; port=5432; dbname=$db; user=$username; password=$password");
            $this->conn->exec("set names utf8");
        } catch (PDOException $exception) {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }

但是我得到了一个错误:

SQLSTATE[08006] [7] 无法将主机名“port=5432”转换为 地址:名称或服务未知

postgresql pdo
© www.soinside.com 2019 - 2024. All rights reserved.