I'm trying to query my DB, and return those values as variables, then print those variables on my custom module's custom template and I can't get it to work.

If I put this into my theme layer it works fine.

$name=db_result(db_query('SELECT name FROM {users} WHERE id = %d', 2));
print $name;

I'd rather keep my queries in the .module file and only have to print the variable on my template. So far I haven't been successful. Also, since I'm planning on having many variables used in many places (like display tables, or as values in calculations) I'd rather be able to print variables where I need them, and not tie them to an $output, or $content variable.

I referenced this post but I couldn't get the example to work.

I'm really new at this, my experience so far as only to take form info and save it to the DB, not bringing it back. so I'd appreciate any help.

  • May you show the code you are using? Your question doesn't give important information, such as in which function would the query you shown be executed.
    – apaderno
    Commented Jan 29, 2013 at 20:09
  • I'd be happy to add more code to help illustrate, I'll have it in in the next hour (as soon as I get home). the php snippet above would be in plain php tags in my template, rather than live within a function, if that helps.
    – StephanieF
    Commented Jan 29, 2013 at 21:08

1 Answer 1


To use the theme API it requires three portions: the template file, the theme hook, and then displaying the defined theme. The below examples are for Drupal 7.

  // defines the theme hook - should follow the naming format "_theme()"
  function mymodule_theme(){
    return array(
      'example_template1' => array(
        'template' => 'mytemplatefile1', // mytemplatefile1.tpl.php
      'example_template2' => array(
        'template' => 'mytemplatefile2', // mytemplatefile2.tpl.php

  // in this function is where you would do your DB queries,
  // and pass to the theme function as shown
  function mymodule_customfunction(){
    $calculation = 1+1;
    return theme('example_template1', array(
      'example_variable1' => $calculation,
      'example_variable2' => '123',

The mytemplatefile1.tpl.php file could look something like this:

<div class="example-markup"><?php echo $example_variable1; ?></div>

To display your output, you could then just execute the following and it would print your value, wrapped in the "example-markup" div.

<?php echo mymodule_customfunction(); ?>
  • how about Drupal 6?
    – StephanieF
    Commented Jan 29, 2013 at 21:25
  • It should be pretty similar to the above example. The variations would be in the hook_theme() function and the theme() function, which can be seen at here and here, respectively.
    – user12923
    Commented Jan 29, 2013 at 22:24
  • Ok thanks! I'll give it a try and let you know how I did. Thank you all for the help so far :)
    – StephanieF
    Commented Jan 30, 2013 at 0:40
  • I'm sorry, I still need help with this. Can anyone point me to some examples for D6?
    – StephanieF
    Commented Feb 4, 2013 at 19:50
  • Thanks all! your examples pointed me in the right direction and I was able to figure it out :) Thanks so much!!
    – StephanieF
    Commented Feb 8, 2013 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.