Print the query results inside the submit function I have a query wherein it fetches data from the database, a simple SELECT query which is processed when a certain submit button is clicked. What I want to achieve is to get the results of that query so that I can populate it on my form. So far my code looks like this,

function my_module_my_form_submit_go($form, &amp;$form_state){
    $values_array = array(
        'week'            =&gt; $form_state['values']['week'],
        'employee_name'   =&gt; $form_state['values']['emp_name'],
    );

    $result = db_query("SELECT * FROM {time} WHERE week='".$values_array['week']."' AND employee_name='".$values_array['employee_name']."' ");
}


and my form looks like this,

// this is where I want to populate the data which came from the database
$form['week'] = array(
    '#type'     =&gt; 'textfield',
    '#title'    =&gt; 'Week',
    '#value'    =&gt; $result['week'],
);

$form['emp_name'] = array(
    '#type'     =&gt; 'textfield',
    '#title'    =&gt; 'Name',
    '#value'    =&gt; $result['employee_name'],
);

$form['go_btn'] = array(
    '#type'           =&gt; 'submit',
    '#title'          =&gt; t('Go'),
    '#value'          =&gt; 'Go',
    '#id'             =&gt; 'go_btn',
    '#submit'         =&gt; array('my_module_my_form_submit_go'),
);


I am pretty sure that the query has results because I ran it on the database. What I want is to echo/print the results in the form because later on they will be also used in another form submission. How would I do such thing after the submit button is clicked. I have only managed to display a text using drupal_set_message but any data which came from the database, I can't display any. Please help. Thanks.
The issue is due to the form submission and rendering process within Drupal.  Long story short, Drupal submits the form then re-renders the form page, which goes through a 'submission' process.  So, all the drupal_set_message calls are getting cleared during this process.

What you want to use is an AJAX callback on the submit button, populating a section of the form with the data retrieved.  And lucky for you the process is much simpler in Drupal 7.

To use the data again in a later form, look at multistep forms.