Important order considerations with multiple databases in Drupal
I have been working recently with a Drupal application where it is necessary to bring in some data from a non-Drupal database. Eventually these features might roll into a module and the data may move into Drupal but for the moment it sits alone in another database.
An hour or so today disappeared into trying to figure out why various theme_ functions did not work properly. Calling theme('item_list', $list); for example returned broken pages and error messages in the log. After searching for what I was doing wrong in calling theme_ it finally hit me that the error log messages were complaining about not finding the system table and not having parts of the theme. From there it was quick to find the information (which I posted as a comment as well).
The upshot is you have to return to the 'default' database before doing theme work. Read the full version for a code examples.
For example this doesn't work properly:
#000000">#0000BB"><?php
db_set_active#007700">(#DD0000">'mydb'#007700">);
#FF8000">//Things to do with mydb
#0000BB">$content #007700">.= #0000BB">theme#007700">(#DD0000">'table'#007700">, #0000BB">$header#007700">, #0000BB">$rows#007700">);
#0000BB">$content #007700">.= #0000BB">theme#007700">(#DD0000">'pager'#007700">, #0000BB">NULL#007700">, #0000BB">10#007700">);
#0000BB">db_set_active#007700">(#DD0000">'default'#007700">);
return
#0000BB">$content#007700">;
#0000BB">?>With that you'll get a broken page that doesn't display the theme properly and some errors in your php error log. If, instead you close the connection to 'mydb' before you call the theme functions. So this code does work:
#000000">#0000BB"><?php
db_set_active#007700">(#DD0000">'mydb'#007700">);
#FF8000">//Things to do with mydb
#0000BB">db_set_active#007700">(#DD0000">'default'#007700">);
#0000BB">$content #007700">.= #0000BB">theme#007700">(#DD0000">'table'#007700">, #0000BB">$header#007700">, #0000BB">$rows#007700">);
#0000BB">$content #007700">.= #0000BB">theme#007700">(#DD0000">'pager'#007700">, #0000BB">NULL#007700">, #0000BB">10#007700">);
return
#0000BB">$content#007700">;
#0000BB">?>Comments
init_theme() will cover you
If you call init_theme() with your database safe, theme() should generally no longer worry about the db. But when it's trying to figure out what theme to load, it does look in the system table.
init_theme()
I ran into this issue as well, when calling across to a different Drupal database. As josh and merlin said, init_theme() is the answer.
Normally the theme system is lazily-initiated, so init_theme() is called the first time you try to theme something, and it then pulls its data out of the database. That's why you normally never have to call it. If your first theme() call happens when you're using a different database, though, then it will initialize out of the foreign database. Calling it manually before you theme anything will make it all play nice.
init_theme() or not...
...the second approach makes a lot more sense regardless. Grab the data from the alien DB and rush as quickly as possible back to Drupal land. The ease of switching databases has made my life certainly a lot easier while keeping Drupal in sync with 5 other DBs that are live, while the Drupal version is being developed behind the scenes. Love it!

