Reputation: 1883
I have the following MySQL table
| module | module_id |
| 301 | 1 |
| 302 | 2 |
| 303 | 3 |
| 304 | 4 |
| 305 | 5 |
| 306 | 6 |
The names of the modules are passed to PHP in the form of a JSON array. I'd like to select and display all of the module IDs which are included in the array. So for example, if the modules 301, 303 and 306 are passed to the script, I want to return the values 1, 3 and 6.
I've put together the following script which almost works, but unfortunately it's only returning the first value - so if 301, 303 and 306 are in the array, the script is returning '1', rather than 1, 3 and 6.
I'm sure there's probably something pretty basic that I'm missing but can't figure out what it is. Can someone help?
Thanks!
$conn = (DATABASE CONNECTION INFO);
$modules = $_REQUEST['modules'];
$insertmodules = json_decode(stripslashes($modules), true);
$inmods = implode(',', $insertmodules);
$getmodsquery = "SELECT module_id FROM modules WHERE module IN ('$inmods')";
$getmods = $conn->query($getmodsquery);
while($row = $getmods->fetch_assoc()) {
foreach ($row as $value){
print_r($value);
}
}
Upvotes: 0
Views: 70
Reputation: 1527
remove quote from query and try this line:
$getmodsquery = "SELECT module_id FROM modules WHERE module IN ($inmods)";
Upvotes: 2