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 tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
Mysql SELECT JOIN tables on two different Databases

Last accessed pages

  1. Sport Games (11907)
  2. Dolphin Olympics 2 (3595)
  3. Dodge Ball (2715)
  4. Theme Hotel (1628)
  5. Nicoles Mommy Challenge (129)

Popular pages this month

  1. Qwop (4068)
  2. Flash Games - Free online Games (2085)
  3. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1836)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (1792)
  5. Drag Racer V3 (1763)