Pull data from external database via term relation

If you need to have Drupal display content in a block that is pulled from an external MySQL database via a taxonomy relationship you can use the following code:

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
 
$nid = (int)arg(1);
 
$sql = "SELECT * FROM term_node WHERE nid = '$nid' LIMIT 1"
 
$result = db_query($sql);
  while (
$anode = db_fetch_object($result)){
 
$tid = $anode->tid;
  }

 
$sql = "SELECT * FROM term_data WHERE tid = '$tid' LIMIT 1"
 
$result = db_query($sql);
  while (
$anode = db_fetch_object($result)){
 
$tname = $anode->name;
  }


db_set_active('legacy');
$sql = "SELECT * FROM departments WHERE name = '$tname' LIMIT 1";
$result = db_query($sql);     

while (
$anode = db_fetch_object($result)) {
     
$output .="<br>$anode->name";
     
$output .="<br><br>$anode->description";
     
$output .="<br><br>$anode->telephone";
     
      } 
     
db_set_active('default');

return
$output;

}


?>

If you have any questions feel free to ask. I am sure this could be done better but this is my first shot at doing this..

Regards,
Phil

 
 

Drupal is a registered trademark of Dries Buytaert.