MYSQL数据库的数据循环

时间:2013-12-18 19:28:28

标签: php mysql

我遇到了数据循环问题。基本上,只要有答案,问题就会循环。我试图显示如下内容:

Question 
Answer 1  
Answer 2 
Answer 3

而不是

Question 
Answer 1

Question
Answer 2

Question
Answer 3

任何人都知道如何解决这个问题?

<?php
$auctionSurvey = "SELECT questions.question_id, answers.question_id, answers.survey_id, question_body, answer_body FROM questions
                  INNER JOIN answers ON answers.question_id = questions.question_id
                  WHERE answers.survey_id='1'";
$aucResult = mysql_query($auctionSurvey) or die (mysql_error());

while ($auctionRow = mysql_fetch_assoc($aucResult)) {
    echo $auctionRow['question_body'] . $auctionRow['answer_body'];
}

1 个答案:

答案 0 :(得分:1)

$questionId = 0;
while($auctionRow = mysql_fetch_assoc($aucResult)){
   if($auctionRow['question_id'] != $questionId){
        echo $auctionRow['question_body'];
        $questionId = $auctionRow['question_id'];
   }  
   echo $auctionRow['answer_body'];
}

添加一些HTML来格式化它,这应该适用于你。

修改:证明:http://ideone.com/Gkq2hd