function dbtng_example_selective_list

Select only certain fields from the database

As with any database query we should only bring in the data we need. DBTNG gives us the field method that expects the table name followed by an array of the fields we want, in this case the table dbtng_example and the fields name and age.

Related topics

1 string reference to 'dbtng_example_selective_list'
dbtng_example_menu in dbtng_example/dbtng_example.module
Implements hook_menu().

File

dbtng_example/dbtng_example.module, line 269

Code

function dbtng_example_selective_list() {
    $output = '';
    // Bring in two fields from the dbtng_example table for the uid 1.
    $select = db_select('dbtng_example')->fields('dbtng_example', array(
        'name',
        'age',
    ))
        ->condition('uid', 1)
        ->execute();
    $rows = array();
    foreach ($select as $entry) {
        // Sanitize the data before handing it off to the theme layer.
        $rows[] = array_map('check_plain', (array) $entry);
        // Make a table for them.
        $header = array(
            t('Name'),
            t('Age'),
        );
        $output .= theme('table', array(
            'header' => $header,
            'rows' => $rows,
        ));
    }
    return $output;
}