0

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.

1
  • Might be worth giving pg-options() a try, but I'm not very sure what data it will give you (nor what data you're actually after). Commented Jan 8, 2013 at 12:19

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Assuming that in a PHP file you encounter the line: $result = pg_query("SELECT author, email FROM authors"); what I am asking is how to get the connection resource used in the pg_query without having to trace back the code to find the last pg_connect...
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 (php-5.4.9\ext\pgsql\pgsql.c). You can get information on the database name or user using pg_dbname or pg_parameter_status

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.