简体   繁体   中英

Connect to PostgreSQL in Cpanel with php

I'm new in this cpanel and I want to ask how to connect to Postgres in cpanel using php?

I use this simple code

<?php

$dbconn = pg_connect("host=localhost port=5432 dbname=test user=domain_test password=test")
or die('connection failed: ' . pg_last_error());

?>

and It keep on returning connection failed on my browser, can somebody tell me how to do it correctly?

You can't really catch connection errors with pg_last_error . You will need to use pg_connection_status for that. But it will not give you enough info to take care of the connection issue.

It looks like error reporting is disabled in your case. So give this a try

error_reporting(E_ALL);
ini_set('display_errors', true);

$dbconn = pg_connect("host=localhost port=5432 dbname=test user=domain_test password=test");

$stat = pg_connection_status($dbconn);
if ($stat === PGSQL_CONNECTION_OK) {
    echo 'Connection status ok';
} else {
    echo 'Connection status bad';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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