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
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

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.