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.$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;
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:
Update:$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;
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."SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers)"
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |