DarV DarV - 1 year ago 87
MySQL Question

PHP mySQL - How to get data from row after using IN

How can i make a query to get access to

inside while loop.

In this case i have two tables:


answer_from // user id
answer_to // question id
answer_choice // user choice


$questions = $db->prepare('SELECT * FROM af_questions WHERE question_id IN (SELECT answer_to FROM af_answers WHERE answer_from = :user_id)');
$questions->bindValue(':user_id', $user_id, PDO::PARAM_INT);

while ($row = $questions->fetch()) {
$question_id = $row['question_id'];

Answer Source

I'm not sure how to answer the question you asked; the question doesn't make sense to me.

It looks to me like what you are really after is the result from a query like this:

  SELECT q.question.id
       , a.answer_choice
    FROM af_questions q
    JOIN af_answers a
      ON a.answer_to = q.question_id
   WHERE a.answer_from = :user_id
   ORDER BY q.question_id

Then in the fetch loop, you'll have access to both question_id and answer_choice.

I'm pretty sure this doesn't answer the question you asked. But if we can backup from that, I'm pretty sure this approach will help you achieve your objectives.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download