user2285115
user2285115

Reputation: 77

Perl sql select all

This is my code

my $results = $dbh->selectall_hashref('SELECT * FROM sample.teachers where term like "$searchterm%"', 'teacher');
my $searchedresults= "";
foreach my $e (keys %$results) {
    print "$e\n";
    print "searching\n";
    $searchedresults = $e;
    }
    print $searchresult."\n";

I am trying to search from a table in mysql server. but seems like it doesn't work I tested it using the hardcoded way by replace $searchterm with a normal string and that works. However I need to use $searchterm but i cannot figure out why it cant work

Upvotes: 0

Views: 127

Answers (1)

ikegami
ikegami

Reputation: 385897

my $results = $dbh->selectall_hashref(
   'SELECT * FROM sample.teachers where term like '.$dbh->quote("$searchterm%"),
   'teacher'
);

or

my $results = $dbh->selectall_hashref(
   'SELECT * FROM sample.teachers where term like ?,
   'teacher',
   undef,
   "$searchterm%"
);

Upvotes: 2

Related Questions