简体   繁体   中英

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

How can i make a query to get access to answer_choice inside while loop.

In this case i have two tables:

af_questions 
    question_id

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

PHP:

$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);
    $questions->execute();

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM