Re: PHP postgres connections - Mailing list pgsql-sql
From | Volkan YAZICI |
---|---|
Subject | Re: PHP postgres connections |
Date | |
Msg-id | [email protected] Whole thread Raw |
In response to | PHP postgres connections (Mauro Bertoli <[email protected]>) |
List | pgsql-sql |
Hi, On 4/29/05, Mauro Bertoli <[email protected]> wrote: > I need to connect to 2 differents Postgres 8.0.0 > databases located in the same machine using the same > PHP script with an "db wrapper object" instance > (pg_Connect)... simply a PHP page with contemporarily > 2 database connections... Firstly some extra information from php.net: {{{ [http://tr.php.net/pg_connect] resource pg_connect ( string connection_string [, int connect_type] ) ... If a secondcall is made to pg_connect() with the same connection_string, no new connection will be established unless you pass SQL_CONNECT_FORCE_NEW as connect_type, but instead, the connection resource of the already opened connection will bereturned. You can have multiple connections to the same database if you use different connection strings. }}} Here's a simple db wrapper class for 2 different db connections: class dbw { /* Connection parameter variables. */ var connParam1; var connParam2; function dbw() { /* Assigning values to conn. params. */ $this->connParam1 = "..."; $this->connParam2 ="..."; } function connect($connParam) { /* Pay attention to SQL_CONNECT_FORCE_NEW parameter. */ return pg_connect($connParam,SQL_CONNECT_FORCE_NEW); } /* ... */ } /* Creating DB Wrapper */ $dbw = new dbw(); /* * If we're not happy with the current connParam1 value: * $dbw->connParam1 = "..."; */ $dbConn1 = $dbw->connect($dbw->connParam1); $dbConn2 = $dbw->connect($dbw->connParam2); > Can I use however persistent connections ? Yep. Just replace pg_connect line in the code with pg_pconnect. But I (as PHP team) don't recommend using persistent connections. Please read "Persistent Database Connections" [1] before deciding to use. [1] http://php.net/manual/en/features.persistent-connections.php Regards. P.S. Next time, please try to use pgsql-php listen for PHP related questions.