I organized the withdrawal of questions from the database, but I can’t correctly write a query or function so that the answers on the question ID will be hooked to it. Now the question is displayed 4 times with each answer. How to fix it? Functions.php file
function getquestions ($conn){ $sql="SELECT questions.question_id,questions.question,answers.answer from questions LEFT JOIN answers ON questions.question_id=answers.question_id"; $result=$conn->query($sql); if ($result->num_rows>0){ while ($row=$result->fetch_assoc()){ $out[]=$row; } return $out; } } File test.php
$data=getquestions($conn); foreach ($data as $testItem) { echo $testItem['question_id']. '. ' . $testItem['question']; echo '<br>'; echo $testItem['answer']; echo '<br>'; }