title: php switching to server connection if localhost failed
body: <p>I'm trying to write a script that connects to an online server if localhost connection failed or inaccessible. I have the script below that connects to a localhost database and if it's not accessible then reroute to a server connection. The script somehow fails to work and I just don't know why. Anyone has any suggestions?</p>
  
  <pre><code>&lt;?php
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = '';
    $DB_NAME = 'admin';
  
    $DB_CON_A = new PDO("mysql:host={$DB_HOST}", $DB_USER, $DB_PASS);
  
    if(!$DB_CON_A) {
      die($DB_CON_A);
      $DB_HOST = 'www.xyz.com';
      $DB_USER = 'admin';
      $DB_PASS = '1235kasK';
      $DB_NAME = 'admin';
    }
  
    try {
      $DB_CON_A = new PDO("mysql:host={$DB_HOST}; dbname={$DB_NAME}", $DB_USER, $DB_PASS);
      $DB_CON_A-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
  
    catch(PDOException $e){
      echo $e-&gt;getMessage();
    }
  </code></pre>