Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
# already connected to database print $cgi->start_form (-method => "POST"); print $cgi->textfield (-name => "plate", -value => $plate, -size => 40); print $cgi->submit (-name => "button", -value => "Search"); print $cgi->end_form (); # run a search if a keyword was specified search_members ($plate) if $plate; # MAIN-BODY print $cgi->end_html (); $dbh->disconnect (); exit (0); # SEARCH_MEMBERS sub search_members { my ($plate) = shift; my ($sth, $count); printf "Search results for keyword: %s<BR><BR>\n", $cgi->escapeHTML ($plate); $sth = $dbh->prepare (qq{ SELECT * FROM plate WHERE genomic_dna_ +id LIKE ? }); $sth->execute ("%" . $plate . "%"); $sth = $dbh->prepare (qq{ SELECT * FROM plate WHERE plate_id LIKE + ? }); # look for string anywhere in name field # the search is performed by adding the % wildcard charcter to + both ends of the keyword so it can be found anywhere in the fname co +lumn. $sth->execute ("%" . $plate . "%"); $count = 0; # fetchrow_hashref returns a reference to a hash containing co +lumn values for the next row of the result set while (my $hash_ref = $sth->fetchrow_hashref ()) { format_html_entry ($hash_ref); ++$count; } print $cgi->p ("$count entries found"); } sub format_html_entry { my ($entry_ref) = shift; my ($address); # encode characters that are special in HTML foreach my $key (keys (%{$entry_ref})) { $entry_ref->{$key} = $cgi->escapeHTML ($entry_ref->{$k +ey}); } printf "<STRONG>Name: %s</STRONG><BR>\n", format_name ($entry_ +ref); print "Plate_id: $entry_ref->{plate_id}<br>\n" if $entry_ref-> +{plate_id}; print "Template_id: $entry_ref->{template_id}<br>\n" if $entry +_ref->{template_id}; print "Genomic_dna_id: $entry_ref->{genomic_dna_id}<br>\n" if +$entry_ref->{genomic_dna_id}; print "Expiry date: $entry_ref->{expiry_date}<BR>\n" if $entry +_ref->{expiry_date}; print "Barcode_ID: $entry_ref->{barcode_id}<BR>\n" if $entry_r +ef->{barcode_id}; }
update (broquaint): added <readmore> tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: simple search + database problem
by zengargoyle (Deacon) on Feb 17, 2003 at 14:56 UTC |