Mysqli_stmt, arrays and the IN operator

I looked through everything, but I can’t find any details about whether it is easy and convenient to pass the array directly to the argument of the IN operator in the prepared MySQL message.

In most places, it is suggested to manually split the array and build the operator, but it will be better.

Whether there is a?

Thank you very much in advance!

EDIT

Sorry, it is not taken into account that this is for the prepared statement via mysqli_stmt in a non-traditional way. :(

0
source share
3 answers
+1
source

Since the IN operator takes values ​​in single quotes ' , separated by a comma ( '1','2','3') or ('alpha','beta','gama') , whereas when using an array in an IN operation and extracting it becomes a string without '

so to use and array in an IN statement you have to do below

 IN (".implode(',',$arr).")") 
0
source

You can use implode() to convert the array to strings,

For instance,

 $array = array(100, 101, 200, 300); $query = 'SELECT * FROM table WHERE id IN ('.implode(',', $array).')'; 
0
source

Source: https://habr.com/ru/post/1239866/


All Articles