简体   繁体   中英

mysqli_stmt, arrays, & IN operator

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. :(

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).")")

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).')';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM