Re: multiple databases with one connection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, April 10, 2005 9:55 pm, Yulius Tjahjadi said:
> Is there a way to change the database after a pg_connect
> happens?  I think in mysql, you can do mysql_select_db('newdb'),
> but there isn't really an equivalent function.
>
> In psql, I can use \connect "newdb", but I think that disconnect
> and reconnects to the new database.

In the psql program, it does (almost for sure) disconnect and reconnect.

But that's not how it is in PHP.

<?php
  $connection = @pg_connect(...);
  $other_db = @pg_connect(...);
?>

This will give you two different database connections to play with,
assuming you get the '...' parts right.

You can echo out $connection and $other_db and see that they are different
resources.

You can use the same technique in MySQL, but it will sometimes give you
the same connection back anyway (grrr!) unless you use the new-fangled
extra argument that requests a new separate connection.

I wasted most of two days tracking down a bug in a colleague's code-base
where he rarely called it from one place that already had my connection,
and he made a "new" connection, and then closed the "new" connection, only
it wasn't *REALLY* a new connection cuz MySQL was just giving him *MY* db
connection.  Grrrr.

-- 
Like Music?
http://l-i-e.com/artists.htm

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux