How to access the external database in drupal? I want to get the values of the current drupal database and the values from an external database in to a same template file. How can I achieve this
First you need to setup your external database as a second database in settings.php.

$databases = array (
  'default' =&gt; 
  array (
    'default' =&gt; 
    array (
      'database' =&gt; 'standard_db',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; 'localhost',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),
  'external' =&gt; 
  array (
    'default' =&gt; 
    array (
      'database' =&gt; 'external',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; 'db.external.com',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),
);


Then before you do your database queries you need to set the active database. In this example, to switch to external you need to execute db_set_active('external');

When you want to switch back to the default database you execute db_set_active();

Example code to switch to and query the external database:

db_set_active('external');
$query = db_select("your_table_name", "t");
$query-&gt;addField("t", "field_name");
$result = $query-&gt;execute();
while ($row = $result-&gt;fetchAssoc()) {
  drupal_set_message($row['field_name']);
}

db_set_active();