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:
-z28my $sth = $dbh->prepare(" SELECT blah FROM blah WHERE blah LIKE ? "); $sth->execute('%foo%');
In reply to Using DBI to create 'loose' search options with LIKE and % by z28
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |