Diom
Diom

Reputation: 121

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?

Upvotes: 1

Views: 2794

Answers (1)

tlenss
tlenss

Reputation: 2609

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';
}

Upvotes: 1

Related Questions