我需要重做从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”转换为 地址:名称或服务未知