title: How to test MySQL connection in PHP and Laravel?
body: <p>I'm making a PHP application installer (something like Wordpress installation script) and I need to check mysql connection using host name, username, password and database provided by user during installation. </p>
  
  <p>I'm using this code as a Laravel controller method to test connection:</p>
  
  <pre><code>public function TestDatabaseConnection(){
      try {
          $database_host = Config::get('config.database_host');
          $database_name = Config::get('config.database_name');
          $database_user = Config::get('config.database_user');
          $database_password = Config::get('config.database_password');
  
          $connection = mysqli_connect($database_host,$database_user,$database_password,$database_name);
  
          if (mysqli_connect_errno()){
                  return false;
              } else {
                  return true;
              }
  
      } catch (Exception $e) {
  
          return false;
  
      }
  }
  </code></pre>
  
  <p>This code doesn't seem to properly test the connection. Function return value (true/false) doesn't depend whether user supplies db data at all, or if db data is correct/incorrect..</p>
  
  <p>Fils /app/config/config.php contains the following array:</p>
  
  <pre><code>&lt;?php return array('database_host' =&gt; 'localhost', 'database_name' =&gt; 'dbasename',    'database_user' =&gt; 'dbuser', 'database_password' =&gt; 'pass');
  </code></pre>
  
  <p>and it's being updated via form during installation process.</p>
  
  <p>Is there any way to modify this code or maybe you have some other code suggestions?</p>