Following up on what moritz said:

First (for mysql, at least) the way to include a literal "%" or literal "_" as part of the search condition in the "LIKE" value is to put a backslash in front of that character -- e.g.  % 1\_000\% % will match any string field value that contains a literal " 1_000% ", with anything (or nothing) before and after.

As a more general method for conjoining different "like" conditions (updated as per "###" comments):

my @sources = ( 'artist 1', 'artist 2', 'whatever' ); ### we make an +array first... my @values = map { s/([_%])/\\$1/g; "%$_%" } @sources; ### ...to avoi +d an error here my $like_clause = join( " or ", map { "Songs.Artist LIKE ?" } @values; my $sth = $dbh->prepare( "SELECT * FROM Songs where ($like_clause)" ); $sth->execute( @values );
(I like putting parens around a set of related "OR" conditions -- it's a handy safeguard in case I need to include some other condition using "AND".)

UPDATE: Sorry about having posted unusable code earlier -- I should have tested my initial version, to see that it produced the error mentioned in Mad_Mac's reply below. My code has been fixed now, as indicated in the comments above.


In reply to Re: SQL LIKE and Binding Variables by graff
in thread SQL LIKE and Binding Variables by Mad_Mac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.