Jump to content

How to php loop with multiple databases


skillednerd

Recommended Posts

I have done select statements where you loop through all the records.

But what I need to do is pull record from one database than insert parts of that record

into the second. The issue I have is no knowing how to select a different db with out

stopping the loop of the first.

 

Thanks in advance. 

Link to comment
https://forums.phpfreaks.com/topic/225436-how-to-php-loop-with-multiple-databases/
Share on other sites

To access two different database servers you would need two different connections. But you can access multiple databases on the same server using a single connection. In fact, you can access multiple databases in a single query:

 

SELECT * 
FROM dbOne.table1 AS T1 JOIN dbTwo.tableBravo AS T2 
  ON T1.coldID = T2.colFK

 

You can also change the default database for a connection at any time using mysql_select_db() (or similar function).

 

Inside your loop, just qualify the INSERT statement to the second database:

INSERT INTO dbTwo.tableName (...) VALUES (...)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.