Viewing Drupal Node Content in a Block

For the CheapFlightSpy.com website, I want to display some text about the departure or destination city in a block (hopefully this will help with SEO). To do this, I created a content type called "citylinks", with a field called "iata" (using the CCK module) for the IATA airport code. The blurb about the city is in the body section of the node. The idea is that when somebody executes a flight search, for example, for flights from Vancouver, I'll find the citylinks content with iata=YVR (the airport code for Vancouver) and display the Vancouver blurb in a block on the flight search results page.

The Views module, although a bit confusing to navigate at first, provides a nice way to select and display node content in a block. However, the options for parameterizing the content selection unfortunately don't work well with the way I've already designed the site. Instead what I want is to write a PHP code snippet that will execute a database query, and place the snippet in the block.

I'm a little weak on writing MySQL queries, so I used the views module to create a block similar to what I want (for a fixed airport code=YVR), and copy pasted the SQL it generated:

SELECT node.nid AS nid,
   node_revisions.body AS node_revisions_body,
   node_revisions.format AS node_revisions_format,
   node.title AS node_title
FROM node node
LEFT JOIN content_type_citylinks node_data_field_iata ON node.vid = node_data_field_iata.vid
LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
WHERE (node_data_field_iata.field_iata_value) = 'YVR'

Next I replaced YVR with a PHP variable $iata (defined earlier in the block), and used the drupal functions db_query to execute the search, and db_fetch_object to get the first result. Finally I passed the node body (which is the blurb about Vancouver) to the check_markup function (to run the content filters on it and convert it to nice html), then echo the result into the block. Here's the complete snippet:


<?php
$sql
="SELECT node.nid AS nid,
   node_revisions.body AS node_revisions_body,
   node_revisions.format AS node_revisions_format,
   node.title AS node_title
FROM node node
LEFT JOIN content_type_citylinks node_data_field_iata ON node.vid = node_data_field_iata.vid
LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
WHERE (node_data_field_iata.field_iata_value) = ('$iata')"
;

$result = db_query($sql);
$node = db_fetch_object($result);

$output=check_markup($node->node_revisions_body);
echo
$output;
?>