By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

how to connect 2 databases simlutaneously using php

100+
P: 245
Hey,
I want to connect to 2 different MYSQL databases at the same time. Does somebody help me out that how can i do that closing one connection and selecting other and vice versa...?


kind regards,
Mohsin Rafique
Jan 20 '10 #1

✓ answered by Atli

Hey.

If you are using the old MySQL extension, then all you would have to do is retrieve the return value of the mysql_connect function and use that value with the other mysql_ functions.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db1 = mysql_connect('host1', 'usr', 'pwd');
  3. $db2 = mysql_connect('host2', 'usr', 'pwd');
  4.  
  5. mysql_select_db('dbName', $db1);
  6. mysql_select_db('dbName', $db2);
  7.  
  8. mysql_query($sql, $db1);
  9. mysql_query($sql, $db2);
  10. ?>

Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hey.

If you are using the old MySQL extension, then all you would have to do is retrieve the return value of the mysql_connect function and use that value with the other mysql_ functions.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db1 = mysql_connect('host1', 'usr', 'pwd');
  3. $db2 = mysql_connect('host2', 'usr', 'pwd');
  4.  
  5. mysql_select_db('dbName', $db1);
  6. mysql_select_db('dbName', $db2);
  7.  
  8. mysql_query($sql, $db1);
  9. mysql_query($sql, $db2);
  10. ?>
Jan 20 '10 #2

dlite922
Expert 100+
P: 1,584
Then one might ask: what if you want to join two tables from two databases together?

easy, MySQL query allows you to select which database a table is from by appending the database number, a dot, and then the table name like so: db1.my_table JOIN db2.second_table.


Food for thought,



Dan
Jan 20 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.