Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

MySQL/Perl LIKE $string question

by peppiv (Curate)
on Jan 31, 2002 at 19:16 UTC ( #142487=perlquestion: print w/replies, xml ) Need Help??
peppiv has asked for the wisdom of the Perl Monks concerning the following question:

This is probably more of a MySQL question.
Let's start with the code:

my $sth = $dbh->prepare('SELECT * FROM applicants WHERE last_name LIKE + "%?%"; ORDER BY date_col, job_position'); $sth->execute($last_name) or die $sth->errstr;

Returns "execute called with 1 bind variables when 0 are needed"

What I'm trying to accomplish is searching the 'applicants' TABLE by column name 'last_name' using the LIKE statement. If I merely place text into this code it works. However, trying to pass a variable into it doesn't work. The $string in the sth works with a placeholder if I don't use LIKE. I've tried escaping the characters, putting the string directly in the LIKE statement and every variation I can think of.

Does anyone have any suggestions? I've looked at all my docs and and found nothing about this.


your lucky lotto numbers are 10 20 30 40 50!

Replies are listed 'Best First'.
Re: MySQL/Perl LIKE $string question
by gav^ (Curate) on Jan 31, 2002 at 19:21 UTC
    You need to use:
    my $sth = $dbh->prepare('SELECT * FROM applicants WHERE last_name LIKE + ? ORDER BY date_col, job_position'); $sth->execute("%$last_name%") or die $sth->errstr;


      As an aside, when using a LIKE clause, there is can be a significant performance difference between   WHERE last_name LIKE 'smith%'
      and   WHERE last_name LIKE ?
      The difference depends on when the database does query planning, and on how sophisticated the query planning is. A sophisticated query planner will look at the first fragment, and determine (by noting no initial wildcard) that if last_name is indexed, the index can be used to optimize the query. However, in the second fragment, the planner can't make any assumptions, and will force a linear scan of the table even if last_name is indexed. For a big table, this can clobber performance.

      I know it work this way with Oracle, but haven't delved into MySQL to this depth. Perhaps someone with more experience can say whether this would also be an issue with MySQL.

      Super thanks gav^! It works. Where did you find this? I hate posting questions when they are so easily answered.


      yes tachyon, been to that site many times and learned a lot! However, it didn't answer this particular question.
      I appreciate it!
        It's probably from how the placeholders are looked for in the query string. Since you're using MySQL, which IIRC doesn't have support for placeholders, that support is faked by the DBI level. Thus, it parses your string but doesn't look inside quoted blocks. So the placeholder in your "%?%" string isn't seen, and thus the DBI engine believes you have no placeholders, and fails on the execute.

        Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
        "I can see my house from here!"
        It's not what you know, but knowing how to find it if you don't know that's important

        Could I recommend this excellent article A short guide to DBI by our own dominus. It should get you on track in no time.




Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://142487]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2018-07-23 17:49 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (472 votes). Check out past polls.