title: Connect Multiple Database with PHP and MySQL
body: <p>We have this multiple database and we need to connect all of them together. We are using relationship table to talk back and forth. Multiple database will help us to backup the project or version separately.</p>
  
  <p><strong>Registration database</strong>: User Log In<br>
  <strong>Project database</strong>: Number of projects under the Logged in user.<br>
  <strong>Version database</strong>: Number of versions under the selected Project</p>
  
  <p><strong>THE ISSUE</strong><br>
  Because we are using <code>0</code> and <code>1</code>, only one user can able to connect to the database at a time. What we need is like the one in the flowchart below. Multiple user can able to connect to the database and working on a different project > different versions. </p>
  
  <p>Thanks!  </p>
  
  <p><em>Sidenote: We are open to try different approach</em></p>
  
  <p><a href="https://i.stack.imgur.com/zZVcN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zZVcN.png" alt="enter image description here"></a> </p>
  
  <pre><code>&lt;?php
  //database error message
   $connect_error='We could not able to connect. Please try later'; 
  
  // Registration table
   $main = mysql_connect('localhost','root','',true) or die($connect_error);
   mysql_select_db('registration-table', $main) ;
  
  // Obtaining session id
  if(isset($_SESSION['id'])===true){  
      $session  = $_SESSION['id'];
  
      // Filtering projects based on 0 and 1
      $sql  = mysql_query("SELECT * FROM project1_table where user_id = '$session' and database_active = '0'",$main);
      $row  = mysql_fetch_array($sql);
  
      // Each Project has Versions
      $project_id = $row['id'];
      $sqli = mysql_query("SELECT * FROM version_table where project_id = '$project_id' and database_active ='0'",$main);
      $emp = mysql_fetch_array($sqli);
  
      // Fetching the database name
      $db_name = $emp['database_name'];
      $sub   = mysql_connect('localhost','root','',true) or die($connect_error);
      mysql_select_db("$db_name", $sub) ;
      }
  ?&gt;
  </code></pre>