title: Multiple mysql database usage and management in php
body: <p>My script requires connections to several databases. Some parts only need to connect to one, which is all fine and dandy, however in some cases I need to perform queries on different database in a single script execution. </p>
  
  <p>Currently I'm doing something like this:</p>
  
  <pre><code>function db_connect($which) {
      if($which == "main") {
          $maindb = mysqli_connect(HOSTNAME_1, USER, PASSWORD, MAIN_DB);
          return $maindb;
      } elseif($which == "stats") {
          $statsdb = mysqli_connect(HOSTNAME_2, USER, PASSWORD, STATS_DB);
          return $statsdb;
      }
  }
  
  
  $maindb = db_connect("main");
  $statsdb = db_connect("stats");
  </code></pre>
  
  <p>I maintain the actual hostname, username, password and db name in a config file full of constants. </p>
  
  <p>I then use the respective links on different queries. </p>
  
  <p>Is there a cleaner way to do this? </p>