0

I have an array called $type which I need to put into an SQL query,

The problem is I dont know how many records $type will have when I submit the query...

$sql="SELECT * FROM tutor_question WHERE questionId in (SELECT questionId FROM tutor_question_list_questions WHERE listId = " . $lid . ")";

Is there any way I can do a foreach loop within the query above to do something like this?

....AND
foreach ($type as $t) {
    echo $t;
}

I need to only extract the questions that fit the specific type I'm looking for.

I have tried a few combinations but cant get anything to work, can anyone help?

3
  • What column are you comparing $type to? Just use IN ([list of values for $type]) Commented Aug 13, 2012 at 12:57
  • I have a column called "questionType". How would I use IN to do this? Commented Aug 13, 2012 at 13:01
  • I couldn't tell you how to use in without knowing the structure of your $type array. Commented Aug 13, 2012 at 13:01

1 Answer 1

3
"questionType IN (" . implode(",",$type) . ")"

You could do something along those lines, providing there is always at least one entry in type, if not you will need to check for this first

Sign up to request clarification or add additional context in comments.

1 Comment

I really hope your $type array is properly escaped.

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.