Connect Drupal to external data base Is there a specific module or a definied mechanism to connect a given external data base to a view?



settings.php

$databases = array (
  'default' =&gt; array (
    'default' =&gt; array (
      'database' =&gt; 'd3js',
      'username' =&gt; 'root',
      'password' =&gt; '',
      'host' =&gt; 'localhost',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),

  'NewDataBase' =&gt; array (
    'default' =&gt; array (
      'database' =&gt; 'NewDataBase',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; '131.188.76.40',
      'port' =&gt; '3306',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ), 
);


This is the module that is related to the view.

 /**
  * @file
  * D3 views module file.
  */

 /**
  * Implements hook_views_api().
  */
  function d3_views_views_api() {
   return array(
   'api' =&gt; 3,
   'path' =&gt; drupal_get_path('module', 'd3_views') . '/views',
  );
 }

 /**
  * Implements hook_theme().
  */
function d3_views_theme() {

   return array(
   'views_ui_style_d3_options_form' =&gt; array(
   'render element' =&gt; 'form',
   ),
 );

}

/**
 * Implements template_preprocess_views_ui_style_d3_options_form().
 */
 function d3_preprocess_views_ui_style_d3_options_form(&amp;$vars) {
   drupal_add_css(drupal_get_path('module', 'd3_views') . '/css/views_ui_style_options_form.css');
 }

/**
 * Theme the form for the table style plugin.
 */
 //declarition of the variables related to form (tree)
 function theme_views_ui_style_d3_options_form($variables) {
 $form = $variables['form'];

 $output = drupal_render($form['library']);

 $header = array(
 t('Field'),
 t('Display'),
 t('Aggregation'),
 t('Data type'),
);
 $rows = array();
 foreach (element_children($form['columns']) as $id) {
 $row = array();
 $row[] = drupal_render($form['columns'][$id]['name']);
 $row[] = drupal_render($form['columns'][$id]['display']);
 $row[] = drupal_render($form['columns'][$id]['aggregate']);
 $row[] = drupal_render($form['columns'][$id]['type']);

 $rows[] = $row;
 }

 $output .= '&lt;fieldset&gt;&lt;legend&gt;Data&lt;/legend&gt;' . theme('table', array('header' =&gt; $header, 'rows' =&gt; $rows)) . '&lt;/fieldset&gt;';

 $output .= drupal_render_children($form);
 return $output;
}


This module also has and I didn't know exactly where to connect to the database.
You will need to add the following configuration in your settings.php file

$databases = array();
$databases['default']['default'] = array(
  // Drupal's default credentials here.
);
$databases['my_other_db']['default'] = array(
  // Your secondary database's credentials here.
);


Refer to settings.php for the array options.

Now if you are using a custom module you can use the following in  your module to switch between the 2 db's

// Use the database we set up earlier
db_set_active('my_other_db');


and if you want to move back to the default db

// Go back to the default database,
// Remember to go back to the drupal default version or drupal will not be able to access its data
db_set_active();


Also refer to https://drupal.org/node/18429 for more information.

There is one more of doing it if you do not want to do the db_set_Active() all the time.

So you define the connection in your settings.php like in the first step. Then in the module you can directly do the following

$query = Database::getConnection('default', 'my_other_db')
           -&gt;select('source_table', 'st')
...