Hi everyone,
for “some” reason the following isn’t working. I have one query which loops out specific numbers(IDs). So there might be 5, 18, 33 ….
I then need to print out the data for each of these Ids, using WHERE id_number = //insert specific number here. What would be the easiest way to do this? Should I first store the numbers in an array?
Thank you very much.
$ids = array(5,18,33);
$ids_comma_separated = implode(',',$ids);
$where = sprintf("WHERE id_number IN (%s)",$ids_comma_separated);
echo $where . "\
";
Note the use of the sql IN parameter which takes a comma separated list of ids.
You need at least one id or the sql parser will generate an error.
Finally, there is no escaping/cleaning going on here. The assumption is that you are generating the list of ids internally so there is no possibility of a sql injection attack.
Hi there ahundiak,
thanks for the help, I appreciate it.
Would you know how I can add the results of the first query to an array? In other words the while loop will return all the numbers which are then assigned to an array.
$r = @mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// fetch all IDs here and assign to an array
}
Thank you!
Yep, just define the array, then add the entry to it.
$r = @mysqli_query($dbc, $q);
$arrayOfIds = array();
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// fetch all IDs here and assign to an array
$arrayOfIds[] = $row['ID'];
}
and a shorter version:
while($row = mysqli_fetch_assoc($r)) {
