I am constructing a MySQL statement for a WHERE IN of varying length like so:
if(count($postTopics)) {
$query = "SELECT `type` FROM `db` WHERE `id` = ".$_POST['id']." AND `topic` IN (";
foreach ($postTopics as $name => $value) {
$query .= "?,";
}
$query .= substr($query,0,-1);
$query .= ")";
}
$result = $mysqli->prepare($query);
if($result === FALSE) {
die($mysqli->error);
}
The $postTopics
variable is a slightly adjusted copy of the $_POST variable. Oddly, this code produces the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT type FROM exam_db WHERE course_id = 3 AND topic IN (?)' at line 1
There are related question regarding binding parameters dynamically, but note that the issue here is not related to bind_param -- the code doesn't make it that far.
Any tips would be much appreciated.