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 is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1"></div></body>';
$dochtml = new DOMDocument();
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    //
Mysql SELECT JOIN tables on two different Databases

Last accessed pages

  1. Insert, Select and Update NULL value in MySQL (41285)
  2. Send POST data with file_get_contents (889)
  3. Get Mime Type of file or string content in PHP (1696)
  4. Adding data from HTML Table Rows in Form fields (5945)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (78378)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1848)
  2. Tabs effect with CSS (1773)
  3. Contact page - CoursesWeb (1768)
  4. Insert, Select and Update NULL value in MySQL (1042)
  5. PHP getElementById and getElementsByTagName (824)