Where to query database in a module I'm creating a module that is basically a form but needs to use information from a table in the database to react the some of the button presses. Rather than executing the database query each time the button is pressed in the callback function, is there a way to retrieve all of the information from the database when the module loads and store it in a variable so that other functions in the module can use/access it?

Thanks in advance

Code: An example module that is similar to my current one. I need to use results from the database query in the copy_text function, and as I build the form up the same results in the callbacks for the other functions, so I don't see a point in putting the same db_query in each callback - surely that's just unnecessary querying of the database:

&lt;?php


function playground_menu() {
  $items['playground/form'] = array(
    'title' =&gt; 'Form',
    'description' =&gt; '',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('playground_demo_form'),
    'access arguments' =&gt; array('access content'),
  );
  return $items;
}

function playground_demo_form($form, $form_state) {

  $form['input'] = array(
    '#type' =&gt; 'textarea',
    '#title' =&gt; t('Input text'),
    '#prefix' =&gt; '&lt;div id="in-text"&gt;',
    '#attributes' =&gt; array(
      'placeholder' =&gt; t('Enter some text here... '),
    ),
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  $form['buttons'] = array(
    '#type' =&gt; 'fieldset',
    '#title_display' =&gt; 'invisible',
    '#attributes' =&gt; array(
      'style' =&gt; 'border: 0px; text-align: right',
      'class' =&gt; array('container-inline'),
    ),
  );

  $form['buttons']['copy'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; 'Copy text',
    '#ajax' =&gt; array(
      'callback' =&gt; 'copy_text',
      'wrapper' =&gt; 'out-text',
//          'method' =&gt; 'replace',
//          'effect' =&gt; 'fade',
    ),
  );

  $form['buttons']['reverse'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; t('Reverse text'),
  );

  $form['buttons']['clear'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; t('Clear text'),
  );

  $form['output'] = array(
    '#type' =&gt; 'textarea',
    '#title' =&gt; t('Output text:'),
    '#prefix' =&gt; '&lt;div id="out-text"&gt;',
    '#value' =&gt; '',
    '#attributes' =&gt; array(
      'placeholder' =&gt; t('Your text will appear here...'),
    ),
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  return $form;
}

function copy_text($form, $form_state){
  $form['output']['#value'] = $form_state['values']['input'];
  return $form['output'];
}
You've got two options, depending on your needs


Static cache - data is built once during a single request and re-used where needed. Your query will run once but once only on every related request. See drupal_static() for example usages.
Backend cache - the results of your database query are cached, and you query the cache bins for the data instead of your database table. Your query will be run once only, until the cache is invalidated, when it'll obviously need to be run again. You'll still incur the overhead of querying the cache, so if your cache is database-based, this may or may not be worth it, depending on the size and complexity of your data. With this method you'll also need to think about how/when/if you invalidate the cache to get fresh data. See the Cache API docs for examples.