Thread: PHP postgres connections
Hi,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... What's the best practice ? Can I use however persistent connections ? Thanks, Mauro B. ___________________________________ Nuovo Yahoo! Messenger: E' molto pi� divertente: Audibles, Avatar, Webcam, Giochi, Rubrica� Scaricalo ora! http://it.messenger.yahoo.it
> Hi, > 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... > I don't think this is the right place to ask this, but there's an example on php.net for using the pg_connect(): <?php $dbconn = pg_connect("dbname=mary"); //connect to a database named "mary" $dbconn2 = pg_connect("host=localhost port=5432 dbname=mary"); // connect to a database named "mary" on "localhost" at port "5432" $dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo"); //connect to a database named "mary" on the host "sheep" with a username and password $conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar"; $dbconn4 = pg_connect($conn_string); //connect to a database named "test" on the host "sheep" with a username and password ?> I don't know if that answers your question. > Can I use however persistent connections ? > pg_pconnect() works the same way. Regards, Yasir
Hi, On 4/29/05, Mauro Bertoli <bertolima@yahoo.it> 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.
Hi, Thank for your answers. I asked here because I had thought the problem was in how Postgres manage connections. Sorry --- Yasir Malik <ymalik@cs.stevens.edu> ha scritto: > > Hi, > > 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... > > > I don't think this is the right place to ask this, > but there's an example > on php.net for using the pg_connect(): > <?php > $dbconn = pg_connect("dbname=mary"); > //connect to a database named "mary" > > $dbconn2 = pg_connect("host=localhost port=5432 > dbname=mary"); > // connect to a database named "mary" on "localhost" > at port "5432" > > $dbconn3 = pg_connect("host=sheep port=5432 > dbname=mary user=lamb > password=foo"); > //connect to a database named "mary" on the host > "sheep" with a username > and password > > $conn_string = "host=sheep port=5432 dbname=test > user=lamb password=bar"; > $dbconn4 = pg_connect($conn_string); > //connect to a database named "test" on the host > "sheep" with a username > and password > ?> > > I don't know if that answers your question. > > > Can I use however persistent connections ? > > > pg_pconnect() works the same way. > > Regards, > Yasir > ___________________________________ Nuovo Yahoo! Messenger: E' molto pi� divertente: Audibles, Avatar, Webcam, Giochi, Rubrica� Scaricalo ora! http://it.messenger.yahoo.it