Greetings Monks! I seek your wisdom!

I set up a basic SQL search using perl's DBI module. As I was searching through my databases' name column I noticed that the search would only match if the contents of the cell were exactly the same. Thus I set out to create a "loose" search option. Here's the jist of my error creating code:

my $name = $cgi->param("name"); my $dbh = DBI->connect('DBI:mysql:my_db', 'user', 'pass') or die "Couldn't open database: $DBI::errstr"; my $sth = $dbh->prepare( "select * from my_db where name like ?" ) or +die "DBI prepare: $DBI::errstr"; $sth->execute( %name% ) or die "Couldn't execute statement: $DBI::errs +tr";

If I want to return a 'strict' result with the above code I change the execution line to:

$sth->execute( $name ) or die "Couldn't execute statement: $DBI::errst +r";

and it works fine. As soon as I take the dollar sign off it stops working.



As far as I can tell my code follows what was laid out in the following code, which was posted to a perl users discussion group:

my $sth = $dbh->prepare(" SELECT blah FROM blah WHERE blah LIKE ? "); $sth->execute('%foo%');
-z28

In reply to Using DBI to create 'loose' search options with LIKE and % by z28

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.