Getting A Node's Information From The Database Table {node}

<?php
function grab_node( $title="", $nid=0 ) {
    if (
$title) {
       
$query = db_query("SELECT * FROM {node} WHERE title LIKE '%s' LIMIT 1", $title);
    } else if (
$nid) {
       
$query = db_query("SELECT * FROM {node} WHERE nid = %d LIMIT 1", $nid);
    } else {
        return
false;   
    }
   
   
$result = db_fetch_object($query);
   
    if (
$result->nid) {       
        return
$result;
    } else {
        return
false;   
    }
}
?>

This function can be placed in your Template.php (if using the PHPTemplate Theme Engine but the PHPTemplate Theme Engine is not required for this function).

Pass either the title($title) or nid($nid) value of the node you wish get.

Example of it being used in node-book.tpl.php

<?php
$node
= grab_node("My Node");

if (
$node) {
  echo
$node->teaser;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.