简体   繁体   中英

PHP Mysqli extension: Using IN keyword in a prepared statement

I'm doing some queries into MySQL database using MySQLi extension in PHP. I need to do a query like this:

SELECT col1, col2 FROM mytable WHERE id IN (2, 4, 6);

This is a concrete example of the query, but the number of ids inside the parenthesis after the IN keyword will vary. Since I want to use the mysqli->prepare() function (prepared statement) I would do it like this:

$statement->prepare("SELECT col1, col2 FROM mytable WHERE id IN (?)");
$statement->bind_pararm("s".... something)

I'm not sure what to put in the bind param function, since i cant know how many IDs will actually be queried for. I couldn't find any examples how to use IN keyword inside prepared statements.

If you prepare your statement once you need it, you could create the placeholders like

join(",", array_fill(0, count($ids) , "?"))

or something.

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