在PHP中迭代Mysql Rows

时间:2012-09-24 05:23:25

标签: php mysql sql

  

可能重复:
  How to iterate by row through a mysql query in 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>

1 个答案:

答案 0 :(得分: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来迭代所有可能的答案。