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


In reply to simple search + database problem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.