0% found this document useful (0 votes)
60 views

Working With Multiple Databases

This script connects to two MySQL databases on different servers, selects tables from each database, and performs a join between the tables to retrieve and display data from both tables. It demonstrates how to connect to multiple databases, select the appropriate one for a query, and join tables across databases in a single SQL query.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Working With Multiple Databases

This script connects to two MySQL databases on different servers, selects tables from each database, and performs a join between the tables to retrieve and display data from both tables. It demonstrates how to connect to multiple databases, select the appropriate one for a query, and join tables across databases in a single SQL query.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php
    $conn = mysql_connect("localhost", "root", "");
    $db1  = mysql_select_db("database1", $conn);
    $db2  = mysql_select_db("database2", $conn);

    $result = mysql_query("SELECT * FROM table1", $db1);


?>

$local = mysql_connect("localhost", "root", "");


$remote = mysql_connect("someremotehost", "username", "");
mysql_select_db("database1", $local);
mysql_select_db("database2", $remote);

$result=mysql_query("SHOW TABLES FROM `database1`;",$local);


$result_remote=mysql_query("SHOW TABLES FROM `database2`;",$remote);

1. php
2.  
3. //Define your database connections and select your database want t
o use. In this example I use two connections and two DBs. But you can use 
more than two.
4.  
5. //MySQL Server 1
6. $dbhost1 = "127.0.0.1";
7. $dbuser1 = "dbuser1";
8. $dbpassword1 = "dbpass1";
9. $db1 = "database1";
10. $connection1 = mysql_connect($dbhost1,$dbuser1,$dbpassword1) or di
e (mysql_error());
11. mysql_select_db($db1,$connection1);
12.  
13. //MySQL Server 2
14. $dbhost2 = "xxx.xxx.xxx.xxx";
15. $dbuser2 = "dbuser2";
16. $dbpassword2 = "dbpass2";
17. $db2 = "database2";
18. $connection2 = mysql_connect($dbhost1,$dbuser1,$dbpassword1) or di
e (mysql_error());
19. mysql_select_db($db2,$connection2);    
20.  
21. //The SQL statement
22. $sql =" SELECT database1.tablename1.fieldname1 AS field1, database
2.tablename2.fieldname2 AS field2 FROM database1.tablename1,database2.tab
lename2";
23.  
24. //Execute query and collect results in $results
25. $results = mysql_query($sql);
26.  
27. //Print result until end of records
28. while($rows = mysql_fetch_array($results)){
29.     print $rows["field1"]." | ".$rows["field2"]."<br>";
30. }

this script connects at two databases, each one located on a different server, and
clones the source_table from the second database to the destination_table located
on localhost (first database)

<?php
$host1="localhost"; // destination
$base1="first_database";
$user1="root";
$password1="";

$host2="192.168.0.1"; //source
$base2="second_database";
$user2="root";
$password2="xxx";

$conection1 = @mysql_connect($host1, $user1, $password1)


or die("Error reaching destination<br>".mysql_error()." nr eroare:
".mysql_errno());
print "Succesfuly connected 1! <br>";

$Db1 = @mysql_select_db($base1, $conection1)


or die("Error reaching destination database:<br>" . mysql_error(). "<br>" .
mysql_errno());
print "Database1 reached!<br>";

$conection2 = @mysql_connect($host2, $user2, $password2)


or die("Error reaching source<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 2!<br>";

$Db2 = @mysql_select_db($baza2, $conexiune2)


or die("Error reaching source database:<br>" . mysql_error(). "<br>" .
mysql_errno());
print "Database2 reached!!<br>";

$query = 'create table destination_table select * from


second_database.source_table';
//echo "<br>".$query."<br>";
$result2 = mysql_query($query2, $conection1) or die('Query failed: ' .
mysql_error().'||'.mysql_errno());

mysql_close($conection1);
mysql_close($conection2);
?>

It's really not difficult to join seperate databases (assuming they reside on the same server)
Just like you would specify fields using the "table.field", you can also use "database.table.field"
Below is an example of a two database join:

[PHP]$sql="SELECT db1.table1.somefield, db2.table1.somefield FROM db1.table1 INNER JOIN


db2.table1 ON db1.table1.someid = db2.table1.someid WHERE db1.table1.somefield =
'queryCrit';"[/PHP]

You might also like