Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am trying to just write a simple search program for my mySQL database. The problem I am having is that I only know how to print the contents of the table row if a particular value is entered e.g. if a correct plate_id is entered. If a correct genomic_dna_id is entered the search can't find anything. How can I improve the search to detect any correct entered value in the 'plate' table??? Any help would be really appreicated.
# 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
    $sth = $dbh->prepare (qq{ SELECT * FROM plate WHERE genomic_dna_ +id LIKE ? }); $sth->execute ("%" . $plate . "%"); # # you need to fetch the first results from this # execute before you do the next execute or they # will get lost! # $sth = $dbh->prepare (qq{ SELECT * FROM plate WHERE plate_id LIKE + ? }); $sth->execute ("%" . $plate . "%"); # # or you could possibly do this and fetch both sets # at the same time. # $sth = $dbh->prepare (qq{ SELECT * FROM plate WHERE plate_id LIKE ? OR genomic_dna_ +id LIKE ? }); $sth->execute ("%" . $plate . "%","%" . $plate . "%");