2012-09-24 50 views
0

可能重複:
How to iterate by row through a mysql query in php迭代通過mysql的排在PHP

我需要遍歷徹底MySQL的行。例如,如果問題ID是1,所有答案都必須在問題1下顯示。我是一名PHP初學者,可以幫助我嗎?

enter image description here


<?php 
      $result = select("SELECT * FROM questions WHERE question_id=1"); 
      $row = mysql_fetch_array($result); 


     ?> 
<table width="581" height="299" border="1"> 
    <tr> 
    <td>Union Assurance Questionnaire</td> 
    </tr> 
    <tr> 
    <td>1.<?php echo $row['questions']; ?> 

    </td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    </tr> 
</table> 
    <?php 
      $result = select("SELECT * FROM questions WHERE question_id=2"); 
      $row = mysql_fetch_array($result); 


     ?> 
<table width="581" height="299" border="1"> 

    <tr> 
    <td>2.<?php echo $row['questions']; ?> 

    </td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    </tr> 
</table> 
+0

什麼答案?沒有任何代碼/表格顯示任何答案,只有名稱中帶有'answer'的screencap中的字段除外。 –

回答

0

使用這樣

<?php 
while($row = mysql_fetch_array($result)) 
{ 
?> 
<table width="581" height="299" border="1"> 
<tr> 
<td>Union Assurance Questionnaire</td> 
</tr> 
<tr> 
<td>1.<?php echo $row['questions']; ?> 

</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
</tr> 
</table> 
<?php 
} 
?> 

你必須使用while() loop用於迭代所有可能的答案。

+0

我已經添加了你的代碼,但第一行問題沒有在頁面上顯示,你有什麼想法?/ –

+0

'第一行問題沒有在頁面上顯示,你能解釋一下嗎? –

+0

對不起,這是我的錯誤,現在正在工作.... –