user1845360
user1845360

Reputation: 857

PHP Postgres default connection

pg_query and pg_query_params in PHP can be executed without specifying a connection resource in the arguments' list, so PHP will use the last connection opene by pg_connect.

Is there a way to retrieve the default connection string that PHP will use intead of trying to figure out where it is coming from across a couple of PHP files? Something like get_default_dbConnection()?

Thanks.

Upvotes: 0

Views: 401

Answers (1)

Hugo Delsing
Hugo Delsing

Reputation: 14173

That doesnt mean that there is a default connection string, but that you dont have to provide a param that references the connection. Check out the example on php: pg_query

$conn = pg_pconnect("dbname=publisher");
if (!$conn) {
  echo "An error occured.\n";
  exit;
}

$result = pg_query($conn, "SELECT author, email FROM authors");
if (!$result) {
  echo "An error occured.\n";
  exit;
}

In this example there is $conn = pg_pconnect("dbname=publisher") and in the call to pg_query they pass a long $conn. That is the connection reference. Now you could also just use

$conn = pg_pconnect("dbname=publisher");
$result = pg_query("SELECT author, email FROM authors");

pg_query will now automatically use the last opened connection (which is $conn)

Update:

You cannot get the resource, PHP uses an internal method php_pgsql_get_default_link to get the default connection but will not return it anywhere (See php-5.4.9\ext\pgsql\pgsql.c in the source).

You can get information on the database name or user using pg_dbname or pg_parameter_status

Upvotes: 2

Related Questions