Preparing the Statement ( cont. )
For example, you might have a DBI script driven via CGI. The
form has checkboxes indicating which selection criteria to use
### Collect the selected field names
@fields = ();
### Work out which checkboxes have been selected
push @fields, "name"     if $nameCheckbox     eq "CHECKED";
push @fields, "location" if $locationCheckbox eq "CHECKED";
### Sanity-check that *something* was selected
die "No fields were selected for querying!\n"
    unless @fields;
### Now build the SQL statement
$statement = sprintf "SELECT %s FROM megaliths WHERE name = %s",
    join(", ", @fields), $dbh->quote($siteNameToQuery);
### Perform the query
$sth = $dbh->prepare( $statement ) or die ...
DBI: The Neophyte's Guide
44