Advertisement
cdsatrian

2 database join connection

May 2nd, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. <?php
  2.  
  3. //Define your database connections and select your database want to 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 = "192.168.34.102";
  7. $dbuser1 = "dbuser1";
  8. $dbpassword1 = "dbpass1";
  9. $db1 = "database1";
  10. $connection1 = mysql_connect($dbhost1,$dbuser1,$dbpassword1) or die (mysql_error());
  11. mysql_select_db($db1,$connection1);
  12.  
  13. //MySQL Server 2
  14. $dbhost2 = "192.168.34.3";
  15. $dbuser2 = "dbuser2";
  16. $dbpassword2 = "dbpass2";
  17. $db2 = "database2";
  18. $connection2 = mysql_connect($dbhost1,$dbuser1,$dbpassword1) or die (mysql_error());
  19. mysql_select_db($db2,$connection2);    
  20.  
  21. //The SQL statement
  22. $sql =" SELECT a.fieldname1 AS field1, "
  23. ."b.fieldname2 AS field2 "
  24. ."FROM database1.tablename1 a"
  25. ."JOIN database2.tablename2 b USING(id)";
  26.  
  27. //Execute query and collect results in $results
  28. $results = mysql_query($sql);
  29.  
  30. //Print result until end of records
  31. while($rows = mysql_fetch_array($results)){
  32.     print $rows["field1"]." | ".$rows["field2"]."<br>";
  33. }
  34.  
  35. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement