Pull data from external database via term relation

Last modified: May 9, 2007 - 03:10

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

Where does that code fit in?

aguitta - March 11, 2009 - 12:32

Hello Phil,

I was wondering where exactly would I put in that code in order for this to work. (Excuse my lack of knowledge on this subject :),
Aguitta

Aguitta - www.aguitta.com

 
 

Drupal is a registered trademark of Dries Buytaert.