在你已经错过了端口号,你的pgsql的连接。 试试这种方式。
<?php
$host = "host=xxx.xx.xxx.21";
$port = "port=5432";
$dbname = "dbname=d106";
$credentials = "user=b16 password=bran";
$connect= pg_connect("$host $port $dbname $credentials" ) or die("Could not connect: " . pg_last_error());
$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo "no results ";
}
while($row = pg_fetch_array($result))
{
$coor = $row['thestartgeom'];
echo $coor;
}
pg_close($connect);
?>
可以存储在一个PHP文件PGSQL连接代码重用
pgsql_db_connection.php文件中的其他PHP文件
<?php
$host = "host=xxx.xx.xxx.21";
$port = "port=5432";
$dbname = "dbname=d106";
$credentials = "user=b16 password=bran";
$connect= pg_connect("$host $port $dbname $credentials" );
if(!$connect){
echo "Error : Unable to open database\n";
}
?>
呼叫pgsql_db_connection.php
文件来使用你的数据库连接。
<?php
require_once('pgsql_db_connection.php');
$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo pg_last_error($connect);
exit;
}
while($row = pg_fetch_array($result))
{
$coor = $row[0];
echo $coor;
}
?>
嘿人,谢谢你的回应。我收到** E_WARNING:类型2 - pg_last_error():尚未打开PostgreSQL链接** –
此外,我不是试图通过本地主机连接,我试图连接到远程pgsql数据库。 –
您是否允许通过配置pg_hba.conf文件来远程连接数据库服务器? –