在一个查询上循环和mysql_fetch_assoc?

PHP*_*VER 2 php mysql html-table while-loop

我有一个在桌面上执行MySQL查询的网页.我有它在表格中回应它并且它应该工作,例如:

$query = mysql_query("SELECT * FROM table");

while($row = mysql_fetch_array($query){
   // my table and db stuff echos out here
}
Run Code Online (Sandbox Code Playgroud)

现在使用相同的mysql查询$ query我试图在同一页面上使用相同的查询在下面回显它但我的问题是它似乎不起作用.

现在你可能会认为我正在做的事情是奇怪的,但之所以上面的第一个代码在表格中回显数据,而且这些东西旁边的复选框就像表格一样回应出来.一切正常,但似乎我不能在同一个查询上面循环上面做另一个.第二个与上面完全相同; 唯一的区别是它不是一种形式.

我只能做一个while(),并mysql_fetch_assoc在一个查询一次?

更新:

对不起,我仍然不理解.

这是我的代码; 谁能为我编辑它?

(我无法在代码中放置php标签以从PHP代码中分离HTML.抱歉给您带来任何不便).

$q = mysql_query("SELECT * FROM table");

<h1> Vote for your favourite extension </h1>

<form method="post" action="<?php echo basename(__file__); ?>">
<table>
<tbody>
    <tr class="odd">
        <td colspan="3" class="cellfeat" style="text-align: center;">Vote for your favourite extension</td>
    </tr>
        <?php
        if(!$q){
            // query failed etc

        } else { // query ok so display form
            while($row = mysql_fetch_array($q)){
                echo '<tr class="odd">';
                echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
                echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
                echo '<td class="cellfeat"><input type="checkbox" name="'.$row['id'].'" value="'.$row['id'].'" /></td>';
                echo '</tr>';
            }
        }
        ?>
</tbody>
</table>
<input type="submit" class="submitcontact" value="Vote" />
</form>

<h1>Extension Statistics</h1>

<table>
    <tbody>
    <tr class="odd">
        <td colspan="3" class="cellfeat" style="text-align: center;">Voting Statistics</td>
    </tr>
            while($row = mysql_fetch_array($q)){
                echo '<tr class="odd">';
                echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
                echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
                echo '<td class="cellfeat">'.$row['count'].'</td>';
                echo '</tr>';
            }
        ?>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

You*_*nse 9

唯一正确的方法如下:
首先,将数据收集到数组中

$data = array();
while($row = mysql_fetch_array($query){
   $data[] = $row;
}
Run Code Online (Sandbox Code Playgroud)

然后,调用模板
并根据需要多次使用您的数据:

<table>
<? foreach ($data as $row): ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['name']?></td>
<? endforeach ?>
</table?>
Run Code Online (Sandbox Code Playgroud)