Drupal: Opening a file download dialog to get the results of a database query without storing the file on the server

I needed a file download dialog in Drupal 6, that creates a file from a database query. The important step was the direct store of the results from the database request without storing the file on the server.
That makes it possible to create a backup of a table from within the system without any additional file storage on the server.

I created a form that contains different radio buttons to select the desired table.

function example_form($form) {
  $form = array();

  //
  // Auswahl der Tabelle
  //
  $form['table'] = array(
    '#type' => 'fieldset',
    '#title' => t('Select a table'),
  );

  $tables = example_get_tables();
  $form['table']['table_selected'] = array(#
    '#type' => 'radios',
    '#options' => $tables,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Download'),
  );

  return $form;
}

The _submit hook of the form performs the database query and creates a string with the desired output of the data. The important part are the two lines with the modified header. These create the file download dialog and determine the name of the target file. Finally, the string will be returned and the reload of the page stopped.

function example_form_submit($form, &$form_state) {
  $table_selected = (string)trim($form_state['values']['table_selected']);

  if (!empty($table_selected)) {

    $results = db_query("SELECT word FROM {" . $table_selected  . "};");
    while ($res = db_fetch_array($results)) {
      // added line break
      $data .= $res['query'] . chr(13) . chr(10);
    }
    drupal_set_header("Content-type: application/x-unknown; utf-8");
    drupal_set_header("Content-Disposition: attachment; filename=" . date('Y-m-d_') . $table_selected . ".csv");
    echo $data;
    exit;
  }
}

No aim, no life.