How to test a database connection? I'm writing a module that will have to make a connection to a seperate database. In order for the user to configure the database, I'm allowing the user to fill out the database connection info in the module configuration form.

How can I test the database connection before submitting the form? 

I added the relevant code below:

function mymodule_form($form, &amp;$form_state) {
  //Form code omitted
  $form['#submit'][] = 'mymodule_form_submit';
  $form = system_settings_form($form);
  return $form;
}

function mymodule_form_validate($form, &amp;$form_state){
  // Validation code, test db connection, need help here.
}

function mymodule_form_submit($form, &amp;$form_state) {
  $values = $form_state['values'];
  $database = array(
    'database' =&gt; $values['database_name'],
    'username' =&gt; $values['database_username'],
    'password' =&gt; $values['database_password'],
    'host' =&gt; $values['database_host'],
    'driver' =&gt; $values['database_driver'],
  );
  Database::addConnectionInfo('mymoduleDB', 'default', $database);
}
I don't see anything in Database or DatabaseConnection specifically. In a pinch you can just run a query that you know won't throw an exception in a Drupal site:

try {
  db_set_active('mymoduleDB');
  db_query('SELECT COUNT(nid) FROM {node}');
}
catch (\PDOException $e) {
  // Berate the user.
}
// PHP5.5+ only
finally {
  db_set_active('default');
}