If you have a list of variables that differs in size every call that you wanto to bind to an IN-statement, the most simple way would be to generate the SQL string programatically and use a loop to bind the variables:
/**
* @param array $values
* @param mysqli $db
* @return mysqli_stmt
*/
function bindInValues(array $values, mysqli $db)
{
$sql = sprintf('SELECT blabla FROM foo WHERE id IN (%s)',
implode(', ', array_fill(0, count($values), '?'))
);
$stmt = $db->prepare($sql);
foreach ($values as $value) {
$stmt->bind_param('s', $value);
}
return $stmt;
}
If you like call_user_func_array you can use dynamic method invocation and go along without a loop.
/**
* @param array $values
* @param mysqli $db
* @return mysqli_stmt
*/
function bindInValues(array $values, mysqli $db)
{
$sql = sprintf('SELECT blabla FROM foo WHERE id IN (%s)',
implode(', ', array_fill(0, count($values), '?'))
);
$stmt = $db->prepare($sql);
array_unshift($values, implode('', array_fill(0, count($values), 's')));
call_user_func_array(array($stmt, 'bind_param'), $values);
return $stmt;
}