If you have a list of variables that differentiates each call you want to associate with an IN -statement in size, the easiest way would be to generate the SQL string programmatically and use a loop to bind the variables:
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 a dynamic method call and go without a loop.
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; }
source share