Drupal how to query a non default db

Sometimes it's necessary to get data from databases other than the default. Here's an example of how I do a simple SELECT query on the non-default database named 'MyOtherDb'.
<?php
$results = db_select('MyOtherDb.mytable', 'mytable')
    ->fields('mytable', array('field1', 'myotherfield'))
    ->condition('field2', 1)
    ->execute()
    ->fetchAll();
?>
Another way to do this would be:
<?php
$conn_temp = db_set_active('MyOtherDb');
$results = db_select('mytable', 'mytable')
    ->fields('mytable', array('field1', 'myotherfield'))
    ->condition('field2', 1)
    ->execute()
    ->fetchAll();
db_set_active($conn_temp);
?>

Article Type

General