1

Is it possible to collect two queries in one while loop?
I have tried the code below, but do not get the expected result.

<?php

    include 'conn.php';
    $query_answers=mysqli_query($conn,"SELECT * FROM answers");


          while($row_answers=mysqli_fetch_array($query_answers)){
    ?>
            <table>
            <tr>
              <td><input type="text" name="" value="<?php echo $row_answers['answer_text'];?>"></td>
              <td><select>
                <option>
                    <?php 
                    $query_question = mysqli_query($conn, "SELECT * FROM questions WHERE question_id= ".$row_answers['next_question_id']."");
                    while($row_answers=mysqli_fetch_array($query_question)){
                        echo $query_question['question_text'];
                    }
                    ?>

                </option>
              </select></td>
            </tr>
                </table>
    <?php

            }

          ?>
2
  • 1
    Possible yes. But if needed it is a sign of a bad DB scheme. You should use joins. The issue you have though is you are using the same variable name so the result set is overwritten. Commented Jun 8, 2018 at 16:38
  • You are defining $row_answers in both loops. I think your inner loop should use $row_questions or something like that instead. And as mentioned above, you could write this in a single query if you would prefer and handle the logic accordingly within the code. Commented Jun 8, 2018 at 16:40

1 Answer 1

1

You are overwriting the row_answers variable instead of using a different one for the question's query. Additionally, you are referencing the query instead of its result in the loop:

while ($row_questions = mysqli_fetch_array($query_question)) {
    echo $row_questions['question_text'];
}
Sign up to request clarification or add additional context in comments.

Comments

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.