Is this what you intended?
$query = " SELECT field1 FROM table1 WHERE field2 IN (1,2,3,4,5) "; $sth1 = $dbh->prepare($query) or die->$errstr(); $sth1->execute($valuesForIn) or die $dbh->errstr;
Often in a case like this, the IN clause would be another SELECT statement. There is not enough info to figure out what that SELECT could be.

You can have  In ($valuesForIn) but of course you will have to prepare the statement again if $valuesForIn changes. You cannot have the $valuesForIn spec'd as runtime bindings using ? because the number or args be specified in advance and must match the number your call with.

I suppose you could have a fixed length of 5 and do something like:

$query = " SELECT field1 FROM table1 WHERE field2 IN (?,?,?,?,?) "; $sth1 = $dbh->prepare($query) or die->$errstr(); $sth1->execute(1,2,3,4,5) or die $dbh->errstr;
Update:
Something like this would be more typical:
"SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers)"
The IN array is derived from an SQL statement and has a variable number of elements in it. Here the number of Countries that have Suppliers is variable and you don't need to know that to successfully prepare and execute the statement.

I think this is a case where you have given us too simple of an example. That seldom happens but appears to be the case here. At bit more details about the application and some context about what 1,2,3,4 stand for would be very helpful.


In reply to Re: SQL prepared statements using MySQL In () by Marshall
in thread SQL prepared statements using MySQL In () by djlerman

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.