Jump to content

conecting to two sep databases with join


redarrow

Recommended Posts

advance thank you.

 

Is this possable to connect to two diffrent database then echo result.

 

if so how do you do it intresting cheers.

 


<?php

$db1=mysql_connect("http://www.database_server1","username1","password1");
mysql_connect("database1",$db1);

$db2=mysql_connect("http://www.database_server2","username2","password2");
mysql_connect("database2",$db2);


$query="select database1.username,database2.surname from userinfo join 
database1.username as a
and database2.surname as b were a='$user_id' and b='$user_id'";

$result=mysql_query($query);

while($list=mysql_fetch_assoc($result)){

echo " ".$list['a']." <br> ".$list['b']." ";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/69283-conecting-to-two-sep-databases-with-join/
Share on other sites

What are your tablenames and column names in the 2 databases?

 

Assuming the tables are

[pre]

database1.users              database2.userinfo

---------------              ------------------

user_id                      user_id

username                      surname

[/pre]

 

Then the query would be

<?php
$sql = "a.username, b.surname
        FROM database1.users a
        INNER JOIN database2.userinfo b ON a.user_id = b.user_id";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res))   
{
    echo $row['username'], ' : ', $row['surname'], '<br />';
}
?>

 

However, I think the 2 databases need to be in the same server

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.