Jump to content

Connecting to two (2) databases at the same time


needs_upgrade

Recommended Posts

Hello again guys!

 

What if i have 2 databases, db1 and db2. i want to read raw data from db1 and match it's content with data from db2, then produce some information. How can i do it at the same time? I know that to be able to access mysql data, we have to use the mysql_connect() and mysql_select_db(). mysql_select_db() only allows to connect to single database. what should i do?

 

thanks for your help in advance.

Link to comment
Share on other sites

mysql_connect returns a link, you will just assign that out and then use that link when querying IE:

 

$db1 = mysql_connect('data', 'data', 'data');
mysql_select_db('database1', $db1);

$db2 = mysql_connect('data2', 'data2', 'data2');
mysql_select_db('database2', $db2);

mysql_query('SOME SQL STATEMENT', $db1);
mysql_query('SOME OTHER SQL STATEMENT', $db2);

 

For a rough example. If it is connecting to the same MySQL Server, you can just invoke the mysql_select_db after you queried the first database and change the database to the second one instead of opening a new connection.

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.