在 PHP 中遍历 Mysql 行 [英] Iterate through Mysql Rows in PHP

查看:39
本文介绍了在 PHP 中遍历 Mysql 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在mysql查询中逐行迭代php

我需要遍历 mysql 行.例如,如果问题 id 为 1,则所有答案都必须显示在问题 1 下.我是 php 初学者,有人可以帮助我吗??

I need to iterate thorough the mysql rows. For an example, if question id is 1 all the answers have to display under the question 1. I am a beginner to php , can any one please help me??

<?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
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() 循环 来迭代所有可能的答案.

You have to use while() loop for iterating all possible answers.

这篇关于在 PHP 中遍历 Mysql 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆