0

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);
  }
 }
3
  • Can you show us output of print_r($insertmodules); ? Commented Mar 5, 2013 at 10:29
  • Do you have an idea what does $getmodsquery content looks like? Commented Mar 5, 2013 at 10:29
  • ...SQL injection as usual Commented Mar 5, 2013 at 10:30

1 Answer 1

2

remove quote from query and try this line:

$getmodsquery = "SELECT module_id FROM modules WHERE module IN ($inmods)";
Sign up to request clarification or add additional context in comments.

1 Comment

That's it! I knew it'd be something embarrassingly simple! Thank you! I'll accept the answer in 10 minutes when it lets me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.