tags:

views:

22

answers:

2

I'm trying to merge to php code bases which each use a different DB. Can I call DB::connect which changing which DB is considered active? Or can I save the active DB and restore it after calling connect?

Edit: The problem is one code base uses mysql_query() without providing $link_identifier, and I don't want to change all the calls, but I still want to be able to open a 2nd DB connection. Right now it works depending on the order in which I connect to the DBs, which is a pain.

+1  A: 

DB::connect creates a connection resource. You can connect to many different databases/servers at the same time. The connection resource contains the database being used. Just store the returned connection reference in different variables and you will be fine.

Connecting to more than one database at the same time is actually very common. For example, connecting to a slave DB for SELECTs and master DB for INSERTs, UPDATEs and DELETEs.

Brent Baisley
+1  A: 

When you create a new connection to the DB, a resource link is usually returned or stored as a member of the DB wrapper class or whatever. You MUST use this link (stored in a variable, for instance) each time you call query functions, etc. If you do not specify a link, the last opened link will be used instead which could be disastrous. If you are diligent about using the correct created link, however, then there will be no problem at all.

tandu