Marcus wrote:
> On 09.07.00 at 15:28 Wesley Darlington wrote:
> > my $search_regex = $dbh->quote($search_string);
> > $search_regex =~ s/\%/\\\%/g; # Escape % signs too
> > $search_regex =~ s/^'/'\%/; # Add a percent at the beginning...
> > $search_regex =~ s/'%/\%'/; # ...and at the end
> > my $query = "SELECT Text FROM Table WHERE Text LIKE $search_regex";
>
> Thanks for all your replies so far.
> What I don't understand is why quote() is necessary?
> Would anybody mind explaining just so I also understand what I'm doing
> here?
>
> This is what's wrong:
> When I want to match the 'title' field and I input the exact string in
> the field (eg. 'Perl'), I get a 100% match using 'LIKE $string', and it
> works.
> However, as soon as I input a part string (eg. 'erl') , I get an error.
> Why does it work with the exact match?
>
> I've tried '%$string%' too, as stated in the Mysql manual. No luck.
> Any ideas what I'm missing here?
>
> Thanks,
>
> Marcus Friedlaender
Try "%$string%" instead.
The single quote will prevent variable expansion.