postgresql connection management

postgresql connection management

am 28.02.2008 19:41:14 von jim white

I am running several web sites on one server, each of which connects to
a different Postgresql database. I use some include files to load
functions and classes. If I put pg_connect at the start of the include
file, then sometimes I do not connect to the correct database from
functions in a class. If I run echo pg_dbname(); in these functions I
see that I am connected to the wrong database. Here are some possible
fixes:

1:
$swdbcon = pg_connect("host=localhost dbname=swgap user=postgres"); //at
start of file
global $swdbcon; // inside function
$result = pg_query($swdbcon, $query);

2:
if (pg_dbname() != "swgap") { //inside function
pg_connect("host=localhost dbname=swgap user=postgres");
}

3:
pg_connect("host=localhost dbname=swgap user=postgres"); inside function

I have already written code that does not explicitly set the connection
in each function and it worked fine. However, I find the problem
occasionally as I change the code. What is the best way to get the
proper connection inside functions?

Jim

--
James (Jim) B. White

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: postgresql connection management

am 29.02.2008 04:30:34 von dmagick

jim white wrote:
> I am running several web sites on one server, each of which connects to
> a different Postgresql database. I use some include files to load
> functions and classes. If I put pg_connect at the start of the include
> file, then sometimes I do not connect to the correct database from
> functions in a class. If I run echo pg_dbname(); in these functions I
> see that I am connected to the wrong database. Here are some possible
> fixes:

The best solution is to work out why it's not connecting to the right db
in the first place, don't do any workarounds.

When it's wrong, what db is it connecting to? Can you work out a pattern?

You don't have a pg_connect() in one file and in another file is another
pg_connect() to a different database?

Do you have any pg_close() calls? When you call pg_query again it might
be after the db connection has been closed and so it creates a new
connection (to the wrong db).

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: postgresql connection management

am 29.02.2008 05:41:17 von jim white

Chris wrote:
> jim white wrote:
>> I am running several web sites on one server, each of which connects
>> to a different Postgresql database. I use some include files to load
>> functions and classes. If I put pg_connect at the start of the
>> include file, then sometimes I do not connect to the correct database
>> from functions in a class. If I run echo pg_dbname(); in these
>> functions I see that I am connected to the wrong database. Here are
>> some possible fixes:
>
> The best solution is to work out why it's not connecting to the right
> db in the first place, don't do any workarounds.
>
> When it's wrong, what db is it connecting to? Can you work out a pattern?
>
> You don't have a pg_connect() in one file and in another file is
> another pg_connect() to a different database?
>
> Do you have any pg_close() calls? When you call pg_query again it
> might be after the db connection has been closed and so it creates a
> new connection (to the wrong db).
>
I don't use pg_close(), I just let PHP close a connection when it
unloads the page. Since I don't really understand why it is picking the
connection to the wrong database, I have decided to be safe and rewrite
all my queries with a named connection:

$result = pg_query($swdbcon, $query);

This is a little extra work, but better safe than sorry.


--
James (Jim) B. White
tel: (919)-380-9615
email: jbw2003@earthlink.net
homepage: http://jimserver.net/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php