pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Volkan YAZICI <volkan.yazici@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: PHP postgres connections
Date: Sat, 30 Apr 2005 11:36:44 +0300
Message-ID: <7104a73705043001361a666de0@mail.gmail.com> (raw)
In-Reply-To: <20050429163134.69632.qmail@web25605.mail.ukl.yahoo.com>
References: <20050429163134.69632.qmail@web25605.mail.ukl.yahoo.com>

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 second call 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 be returned. 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.



view thread (4+ messages)  latest in thread

Message-ID: <7104a73705043001361a666de0@mail.gmail.com>
Permalink:  ../7104a73705043001361a666de0@mail.gmail.com/
Also on:    postgresql.org/message-id/7104a73705043001361a666de0@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: volkan.yazici@gmail.com
  Subject: Re: PHP postgres connections
  In-Reply-To: <7104a73705043001361a666de0@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox