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
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 (...)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.