2013-12-18 44 views
0

我在這裏遇到數據循環的問題。基本上,只要有答案,問題就會持續循環。我想顯示是這樣的:來自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']; 
} 
+0

因爲這是如何'JOIN's工作。如果您的數據在表格A和表格B之間以n:m關係進行映射,那麼您將擁有n個B數據副本,m個副本數據和n * m個總行數。你必須用PHP編寫邏輯來處理它。 – Sammitch

回答

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

+0

這隻顯示answer_body權利?我怎樣才能讓問題不會循環? 問題答案1答案2答案3 而不是 問題答案1問題答案2 ... – user3103739

+0

你試過了嗎?你甚至讀過代碼嗎?你剛纔問了你的問題,回答我的答案。 – Jessica

+0

是的,我確實嘗試過。它只顯示答案。我嘗試回顯question_body,但似乎沒有工作。 – user3103739