mysqli_stmt, arrays, & IN operator

梦想的初衷 提交于 2019-12-12 05:47:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!