Skip to content Skip to navigation

How to connect Drupal to an external DB

Posted by: 
Topics: 
via Matt Cheney, ChapterThree

The best way to connect Drupal to an external MySQL database is to define the $db_url in the settings.php as an array of possible databases and then using the function db_set_active($database_name) to switch between them in PHP. There is some good documentation of this technique on Drupal.org here - http://drupal.org/node/18429.

To allow your users to interact with this data, you could presumably build a module that returns the data in a variety of ways - including making tables, lists, etc. However, the best "Drupal Way" to do this involves a technique called "Lazy Instantiation" (http://en.wikipedia.org/wiki/Lazy_instantiation) which will create CCK nodes on the fly when externally requested using the Drupal function drupal_execute() http://api.drupal.org/api/function/drupal_execute to submit the node add form for your custom CCK type.