pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PHP postgres connections
4+ messages / 3 participants
[nested] [flat]

* PHP postgres connections
@ 2005-04-29 16:31  Mauro Bertoli <bertolima@yahoo.it>
  0 siblings, 2 replies; 4+ messages in thread

From: Mauro Bertoli @ 2005-04-29 16:31 UTC (permalink / raw)
  To: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PHP postgres connections
@ 2005-04-30 03:47  Yasir Malik <ymalik@cs.stevens.edu>
  parent: Mauro Bertoli <bertolima@yahoo.it>
  1 sibling, 0 replies; 4+ messages in thread

From: Yasir Malik @ 2005-04-30 03:47 UTC (permalink / raw)
  To: Mauro Bertoli <bertolima@yahoo.it>; +Cc: pgsql-sql

> 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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PHP postgres connections
@ 2005-04-30 08:36  Volkan YAZICI <volkan.yazici@gmail.com>
  parent: Mauro Bertoli <bertolima@yahoo.it>
  1 sibling, 0 replies; 4+ messages in thread

From: Volkan YAZICI @ 2005-04-30 08:36 UTC (permalink / raw)
  To: pgsql-sql

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.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: PHP postgres connections
@ 2005-05-02 09:10  Mauro Bertoli <bertolima@yahoo.it>
  0 siblings, 0 replies; 4+ messages in thread

From: Mauro Bertoli @ 2005-05-02 09:10 UTC (permalink / raw)
  To: Yasir Malik <ymalik@cs.stevens.edu>; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-05-02 09:10 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-29 16:31 PHP postgres connections Mauro Bertoli <bertolima@yahoo.it>
2005-04-30 03:47 ` Yasir Malik <ymalik@cs.stevens.edu>
2005-04-30 08:36 ` Volkan YAZICI <volkan.yazici@gmail.com>
2005-05-02 09:10 Re: PHP postgres connections Mauro Bertoli <bertolima@yahoo.it>

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