我有一個網頁,在表上執行MySQL查詢。我把它呼應出來的表和作品,因爲它應該,例如:while循環和mysql_fetch_assoc在一個查詢上?
$query = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($query){
// my table and db stuff echos out here
}
現在使用相同的MySQL查詢$查詢我想再次呼應它使用在同一頁上同樣的查詢下面,但我的問題是它似乎並沒有工作。
現在你可能會認爲我在做什麼很奇怪,但是爲什麼上面的第一個代碼會在表中回顯數據,而且旁邊的複選框會回顯,因爲它是表單。所有這一切工作正常,但似乎我不能再做同樣的查詢上面的循環。第二個與上面完全一樣;唯一的區別是它不是一種形式。
我只能在單個查詢中執行一次while()
和mysql_fetch_assoc
嗎?
UPDATE:
對不起,我還是不正確理解。
這是我的代碼;任何人都可以爲我編輯它?
(我無法在代碼中放置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>
哦。你的代碼都是錯的。你在哪裏得到它?你有沒有工作代碼? – 2011-02-28 14:11:18
現在已經解決了,謝謝。它工作得很好,而不是第二次查詢。但我用mysql_data_seek(),它做我想要的。謝謝phplover – PHPLOVER 2011-02-28 14:25:14