简体   繁体   English

PostgreSQL与PHP的连接不起作用

[英]PostgreSQL connection with PHP not working

I am trying to develop a basic page that uses PostgreSQL for the database. 我正在尝试开发一个将PostgreSQL用于数据库的基本页面。 The database is already set. 数据库已设置。 Right now I was simply trying to create a connection with it. 现在,我只是试图与其建立连接。

<?php
    echo "test 1";
    $dbconn = pg_connect("host='localhost' port='5432' dbname='demorole' user='demorole' password='eta'") or die ("couldnt connect");
    echo "test 2";
?>

demorole is my database and its owner user. demorole是我的数据库及其所有者用户。 eta is a sample of password (but is correct) eta是密码示例(但正确)

My problem is that this page doesn't work at all. 我的问题是此页面根本无法使用。 It doesn't display any errors. 它不会显示任何错误。 When i use the function pg_connection_status() it doesn't return anything. 当我使用function pg_connection_status()它不返回任何内容。 It only prints "test 1" (the echo commands are for debugging purposes). 它仅打印“ test 1”( echo命令用于调试目的)。

I am working on Linux Mint. 我正在使用Linux Mint。 Both PostgreSQL and Apache are running. PostgreSQL和Apache都在运行。 The connection info are correct. 连接信息正确。 Any idea what might be wrong? 知道有什么问题吗? Because, since it doesn't display any errors, I'm stuck. 因为,由于它不显示任何错误,所以我被卡住了。

Make sure you have pgsql php extension installed. 确保您已安装pgsql php扩展。

And turn on error displaying in php.ini 并打开php.ini中显示的错误

display_errors = On display_startup_errors = On

This code worked. 此代码有效。

 $host        = "host=127.0.0.1";                         
 $port        = "port=5432";                        
 $dbname      = "dbname=mydb";                         
 $credentials = "user=myrole password=mypsw";

$connection = pg_connect( "$host $port $dbname $credentials"  );
if(!$connection){  echo "Error : Unable to open database\n"; } 
 return $connection;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM