You are here: Re: [PHP] multiple databases with one connection « PHP « IT news, forums, messages
Re: [PHP] multiple databases with one connection

Posted by Richard Lynch on 04/12/05 01:20

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация