【问题标题】:Iterate through Mysql Rows in PHP [duplicate]遍历PHP中的Mysql行[重复]
【发布时间】:2012-09-15 13:43:18
【问题描述】:

可能重复:
How to iterate by row through a mysql query in php

我需要遍历 mysql 行。例如,如果问题 id 为 1,则所有答案都必须显示在问题 1 下。我是 php 的初学者,谁能帮帮我??


<?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>

【问题讨论】:

  • 什么答案?您的代码/表格都没有显示任何答案,除了屏幕截图中名称中带有“答案”的字段。

标签: php mysql sql


【解决方案1】:

这样使用

<?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 来迭代所有可能的答案。

【讨论】:

  • 我已经添加了你的代码,但是页面上没有显示第一行的问题,你知道吗??/
  • first row question is not showing on the page,你能解释一下吗??
  • 对不起,那是我的错误,它现在正在工作......
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 2021-01-11
  • 2015-05-16
  • 1970-01-01
  • 2021-12-18
  • 2019-07-19
  • 2016-08-24
  • 2013-02-02
相关资源
最近更新 更多