For a searchfunction on my site, I use the LIKE-method to get the right information out of my database. I've broken up the searchterm into apart words, and they have become the $ucTerms variables.
Now everything works fine, but when I remove the % (because that's what I want, I want to search for the exact words, not words containing the variable) it doesn't work anymore. My question is: can't I use the LIKE without the %? And what can I use to make it work then?
Code:
if (!$ucTerms[1]){
my $sth = $dbh->prepare("SELECT * FROM metalink WHERE `inhoud` LIKE '%$ucTerms[0]%'") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
my $linkid = $results->{id};
my $url = $results->{url};
my $titel = $results->{titel};
my $inhoud = $results->{inhoud};
$deelsearchresults{$linkid} = "$linkid|$url|$titel|$inhoud";
} $sth->finish;
}
elsif (!$ucTerms[2]){
my $sth = $dbh->prepare("SELECT * FROM metalink WHERE `inhoud` LIKE '%$ucTerms[0]%' AND `inhoud` LIKE '%$ucTerms[1]%')") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
my $linkid = $results->{id};
my $url = $results->{url};
my $titel = $results->{titel};
my $inhoud = $results->{inhoud};
$deelsearchresults{$linkid} = "$linkid|$url|$titel|$inhoud";
} $sth->finish;
}
elsif (!$ucTerms[3]){
my $sth = $dbh->prepare("SELECT * FROM metalink WHERE `inhoud` LIKE '%$ucTerms[0]%' AND `inhoud` LIKE '%$ucTerms[1]%')") AND `inhoud` LIKE '%$ucTerms[2]%')") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
my $linkid = $results->{id};
my $url = $results->{url};
my $titel = $results->{titel};
my $inhoud = $results->{inhoud};
$deelsearchresults{$linkid} = "$linkid|$url|$titel|$inhoud";
} $sth->finish;
}
elsif (!$ucTerms[4]){
my $sth = $dbh->prepare("SELECT * FROM metalink WHERE (`inhoud` LIKE '%$ucTerms[0]%' AND `inhoud` LIKE '%$ucTerms[1]%' OR `inhoud` LIKE '%$ucTerms[2]%' OR `inhoud` LIKE '%$ucTerms[3]%')") || print "could not access database";
$sth->execute();
while (my $results = $sth->fetchrow_hashref) {
my $linkid = $results->{id};
my $url = $results->{url};
my $titel = $results->{titel};
my $inhoud = $results->{inhoud};
$deelsearchresults{$linkid} = "$linkid|$url|$titel|$inhoud";
} $sth->finish;
}
Now everything works fine, but when I remove the % (because that's what I want, I want to search for the exact words, not words containing the variable) it doesn't work anymore. My question is: can't I use the LIKE without the %? And what can I use to make it work then?