Answer the question
In order to leave comments, you need to log in
How to replace a string with numbers in the IN operator?
I need to take an array of users except id 8,10,11 and so on
. My query looks like this:
SELECT *
FROM `users`
WHERE id not in(?)
Answer the question
In order to leave comments, you need to log in
The numbers here are just completely optional, but there should be more question marks. For every go.
PHP is not a genie out of a bottle to guess what was meant here - the whole string or individual values.
If you need separate values, then you need to transfer them separately , n'est pas?
It certainly doesn't look as pretty, but there are still no other options.
$array = [8,10,11];
$in = str_repeat('?,', count($array) - 1) . '?';
$sql = "SELECT * FROM users WHERE id NOT IN ($in)";
$stmt = $db->prepare($sql);
$stmt->execute($array);
If the passed parameter is a CSV of identifiers, then you should use
SELECT *
FROM `users`
WHERE FIND_IN_SET(id, ?);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question