I have 2 tables with a one-to-many relationship.
Table 1:
_________________
| id | question |
|____|___________|
| 1 | question1 |
| 2 | question2 |
|____|___________|
Table 2:
______________________________
| id | answer | question_id |
|____|_________|_____________|
| 1 | answer1 | 1 |
| 2 | answer2 | 1 |
|____|_________|_____________|
While question_id
is the foreign key.
I'm trying to show the data like that:
question1
answer1 answer2
So question then the answers for this particular question.
Here is the code I have:
$query = "SELECT questions.*, answers.* FROM questions INNER JOIN answers ON questions.id = answers.question_id";
$run = mysqli_query($connect, $query);
while($data = mysqli_fetch_assoc($run)){
echo $data['question'] . '<br>';
echo $data['answer'] . '<br>';
}
This returns:
question1
answer1
question1
answer2
So how to show the question only once with the related answers to this particular question?