title: PHP - Left join from two tables in different databases with different credentials using PDO
body: <p><em><strong>Please note:</strong> While my original issue was not possible to be solved in the way I expected, @Bamar solution marked in this post is an alternative that reaches the same goal and works perfectly. What I proposed in this post to be done doesn't seem to be viable if the databases are located in different hosts.</em></p>
  <p>I've been searching for a while and I seem to be unable to solve my issue.</p>
  <h2>THE DATA I HAVE</h2>
  <p>My service provider is 1&amp;1. In the current contract I have with them I could create up to 100 databases with a maximun size of 2GB each.<br />
  Each database that is created, is assingned a random hostname, port and username (the only item which I can choose is the password).<br />
  I've got two different databases, lets call them <strong>DB_1</strong> and <strong>DB_2</strong>.<br />
  In the <strong>DB_1</strong> I've got a table called <strong>T_USERS</strong> which fields of interest for this particular problem are:<br /></p>
  <ul>
  <li><strong>ID</strong>: The ID of the record.<br /></li>
  <li><strong>userName</strong>: The user name registered on the database.<br /></li>
  </ul>
  <p>In the <strong>DB_2</strong> I've got a table called <strong>T_SCORES</strong> which fields of interest for this particular problem are:<br /></p>
  <ul>
  <li><strong>ID_User</strong>: it's a foregin key that refers to the ID of a particular user in <strong>DB_1.T_USERS</strong><br /></li>
  <li><strong>score</strong>: a numeric value that indicates the score of that user.<br /></li>
  </ul>
  <p><em>It is important to take into account that to access both databases each of them needs different credentials!</em>
  <br /></p>
  <h2>WHAT I WANT TO ACHIEVE</h2>
  <p>What I want to achieve seems simple at a first glance but I was unable to find any documentation or solution online on how to do this using PHP and PDO.<br />
  I just want to perform a join with DB_2.ID_USER and DB_1.ID<br />
  My final result should look something like this:<br /></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">DB_1.userName</th>
  <th style="text-align: center;">DB_2.score</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">Alex</td>
  <td style="text-align: center;">237</td>
  </tr>
  <tr>
  <td style="text-align: center;">Peter</td>
  <td style="text-align: center;">120</td>
  </tr>
  <tr>
  <td style="text-align: center;">Mark</td>
  <td style="text-align: center;">400</td>
  </tr>
  <tr>
  <td style="text-align: center;">...</td>
  <td style="text-align: center;">...</td>
  </tr>
  </tbody>
  </table>
  </div><br />
  <h2>WHERE I'M CURRENTLY STUCK</h2>
  <p>This is what I've currently tried.<br />
  First of all I perform the connection to my databases as follows (I normally use a try/catch when connecting to a DB but I will omit it here):<br /></p>
  <pre><code>//Connection to the DB1
  $db1_hostName = &quot;hostnameofDB1&quot;;//The host name of the database 1
  $db1_name = &quot;db1&quot;;//The name of the database 1
  $db1_userName = &quot;user1&quot;;//The username in the database 1
  $db1_password = &quot;pw1&quot;;//The password for the database 1
  $pdo_db1Handle = new PDO(&quot;mysql:host=$db1_hostName; dbname=$db1_name;&quot;, $db1_userName, $db1_password);
  $pdo_db1Handle-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  //Connection to the DB2
  $db2_hostName = &quot;hostnameofDB2&quot;;//The host name of the database 2
  $db2_name = &quot;db2&quot;;//The name of the database 2
  $db2_userName = &quot;user2&quot;;//The username in the database 2
  $db2_password = &quot;pw2&quot;;//The password for the database 2
  $pdo_db2Handle = new PDO(&quot;mysql:host=$db2_hostName; dbname=$db2_name;&quot;, $db2_userName, $db2_password);
  $pdo_db2Handle-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  </code></pre>
  <p>So basically up to this point what I've done is very simple, create a pdo_db1Handle and pdo_db2Handle. Now to the tricky part...<br />
  If I now want to perform a join my SQL syntax should be something like this:<br /></p>
  <pre><code>SELECT DB_1.T_USERS.userName, DB_2.T_SCORES.score
  FROM DB_2.T_SCORES 
      LEFT JOIN DB_1.T_USERS 
      ON (DB_2.T_SCORES.ID_User=DB_1.T_USERS.ID)
  ORDER BY DB_2.T_SCORES.score ASC 'The ordering is optional, I'm interested in the join part first
  </code></pre>
  <p>But as far as I'm aware and with all the information I was able to find, you execute the SQL statement against one of the two handles I previously defined in the following way:</p>
  <pre><code>$stmt=$pdo_db1Handle-&gt;prepare($mySQLStatement);
  $stmt-&gt;execute();
  </code></pre>
  <p>When I try to do this, an error shows up telling me missing credentials for the DB_2. It happens the opposite (missing credentials of DB_1) if I try to execute it against pdo_db2Handle.<br /><br />
  How should I proceed? any solution using PDO for this?
  <br /><br /><br />
  <em><strong>Thanks in advance :)</strong></em> <br /><br /><br /></p>