The code example from this page shows how to make a MySQL SELECT with JOIN table of different database, using PDO.
The trick is to specify both the database name and table in the SQL query, by using the syntax: database_name.table_name.
- This method works if you use an user and password for connecting to MySQL that has access to both databases.

Complete code example

// Data for connecting to MySQL
$mysql =['host'=>'localhost', 'dbname'=>'db_1', 'user'=>'root', 'pass'=>'password'];

//connects with PDO 
try {
  $conn = new PDO('mysql:host='. $mysql['host'] .'; dbname='. $mysql['dbname'], $mysql['user'], $mysql['pass']);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //set the PDO error mode to exception
catch(PDOException $e){ exit('Connection failed: '. $e->getMessage());}

$sql ="SELECT,, t2.col_db2 FROM db_1.table1 AS t1
 LEFT JOIN db_2.table2 AS t2 ON =";
$stmt = $conn->query($sql);

if($stmt !== false) {
  //shows selected data
  foreach($stmt as $row) {
    echo $row['id'] .'--'. $row['name'] .'--'. $row['col_db2'] .'<br>';
else {
  echo '0 results';
- In this way you can join multiple tables from different databases.

Daily Test with Code Example

Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="" title=""></a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
Mysql SELECT JOIN tables on two different Databases

Last accessed pages

  1. The Rise Of Atlantis (5168)
  2. Volume and Surface Area Calculator for 3D objects (5928)
  3. Dog Mahjong (3485)
  4. Bloons Tower Defense 4 (1173)
  5. Read Excel file data in PHP - PhpExcelReader (41452)

Popular pages this month

  1. Qwop (4763)
  2. Bubbles3 (4405)
  3. Butterfly Kyodai (3134)
  4. Zuma Deluxe (2904)
  5. Backgammon (1486)