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 t1.id, t1.name, t2.col_db2 FROM db_1.table1 AS t1
 LEFT JOIN db_2.table2 AS t2 ON t1.id = t2.id";
$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

HTML
CSS
JavaScript
PHP-MySQL
Click on the tag that creates an ordered list
<ul> <tr> <ol>
<ol>
  <li>List-1</li>
  <li>List-2</li>
</ol>
Which selector represents the ID of an element in CSS
.name #name name
#id {
  color: #0110fb;
}
What statement creates an object in JavaScript?
{} [] new Date()
var obj = {"site": "CoursesWeb.net", "pr": 5};
alert(obj.site);
Indicate the instruction used to evaluate if a condiition is True or False
else if() switch()
$var = 8;
if($var == 8) echo $var;
Mysql SELECT JOIN tables on two different Databases

Last accessed pages

  1. Drag Racer V3 (948)
  2. The Number and Math Objects (344)
  3. Download JavaScript resources (2345)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33344)
  5. Recursive function to create Multi-Level Menu in PHP (4811)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49682)
  2. Read Excel file data in PHP - PhpExcelReader (38072)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (37003)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33344)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30742)