Forum Moderators: coopster

Message Too Old, No Replies

Get Data From Two Databases

how can i get data from two databases in one query?

         

carolreza

3:22 am on Sep 1, 2009 (gmt 0)

10+ Year Member



Hello..
I have connect two database with

//COONNECTION 1
$link1 = mysql_connect( $host1, $user1, $pass1 );
mysql_select_db( $DB1, $link1 );
$query = "SELECT * FROM table1indatabase1";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) )
{
echo $row[0];
}

//COONNECTION 2
$link2 = mysql_connect( $host2, $user2, $pass2 );
mysql_select_db( $DB2, $link2 );
$query = "SELECT * FROM table2indatabase2";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) )
{
echo $row[0];
}

those are connecting 2 database.
I use a mysql database class to connect the databases
$db1 = new database( $host1, user1, $pass1, $db1 );
$db2 = new database( $host2, user2, $pass2, $db2 );

and my question is
How can get data from 2 database in one query, and how to connect two database in one object class ?

I have a query like this :
$query = "SELECT database1.table1.id FROM database1.table1
WHERE database1.table1.foreign_id = database2.table2.id";

Thanks for your help guys!

omoutop

5:51 am on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



as far as i know, you need a user that has access to both databases.
Use the GRANT option to gain such priviledges (or ask your local admin to do so)

After that you can join normaly:
Select a.onefield, b.secondfield FROM db1.table1 a, db2.table2 b WHERE a.id=b.id (for example)