问题
I've looked all over, but I can't find any details on whether or not it's possible to easily and conveniently pass an array directly into the argument of the IN operator in a MySQL prepared statement.
Most places suggest manually breaking up the array and building the statement, but there's got to be a better way.
Is there?
Many thanks in advance!
EDIT
Sorry, left out that this is for a prepared statement via mysqli_stmt not the traditional way. :(
回答1:
Because IN operator takes values in single quoted '
comma separated string ( '1','2','3')
or ('alpha','beta','gama')
whereas when we use array in IN operation and extract then it become a string without '
so to use and array in IN operator you should do below way
IN (".implode(',',$arr).")")
回答2:
You can use implode()
to convert array to strings,
For eg:
$array = array(100, 101, 200, 300);
$query = 'SELECT * FROM table WHERE id IN ('.implode(',', $array).')';
回答3:
Found this MySQLi Bind Param with an array for IN
Which lead to this How to bind mysqli bind_param arguments dynamically in PHP? which I couldn't get to work.
Then saw this Can I bind an array to an IN() condition?
PDO > MySQLi
来源:https://stackoverflow.com/questions/13244361/mysqli-stmt-arrays-in-operator