[英]Stop continuous table generation
我試圖阻止下表中的數據繼續重復自身,就像在循環訪問相同數據時一樣,導致瀏覽器崩潰:
這是查詢:
$posts = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from tbl_bugresponse WHERE bugid = $id ORDER BY time"));
這是表代碼:
<table width="100%" class="tborder">
<tr>
<th class="tcat">Name</th>
<th class="tcat">Reply</th>
</tr>
<?php
$colour="1";
while($posts) {
if($colour == "1"){
echo ("<tr class='alt1'>");
$f1 = $posts['by'];
$f2 = $posts['content'];
$colour = "2";
}
else {
echo "<tr class='alt2'>";
$f1 = $posts['by'];
$f2 = $posts['content'];
$colour = "1";
}
?>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
</tr>
<?}?>
</table>
請注意,我使用的是mysqli而不是mysql。
問題是while($posts){
行。
您正在編寫代碼,上面寫着“繼續前進,直到有人將posts變量設置為null或false為止”,但您再也不會碰它……因此它將永遠持續下去。
更新資料
再次查看您的查詢,您似乎對它應該如何工作感到困惑。 它應該是這樣的:
$result = mysqli_query($db, "SELECT * from tbl_bugresponse WHERE bugid = $id ORDER BY time");
...
$post = mysql_fetch_assoc($result); // fetch first row
while( $post ) { // will keep going until $post gets set to null or false
...
$post = mysql_fetch_assoc($result); // fetch next row and re-run the loop
}
您會注意到mysqli_query
調用已被分離出來。 mysqli_query
作用是運行數據庫查詢,然后為您提供從查詢返回的行列表的“句柄”。 這是常見的混淆點,因為大多數人希望它能為他們提供所有行的數組,而不是“句柄”。
然后,您必須調用mysql_fetch_assoc
從列表中取出第一行,對其進行處理,然后再次重復調用mysql_fetch_assoc
以獲取下一行。
當它用完所有行時, mysql_fetch_assoc
將返回null
而不是有效行,因此您可以通過這種方式查看完成的時間並停止循環。
您並沒有完成結果集。 mysqli_query獲取結果集,並且mysqli_fetch_assoc逐步進行搜索。 您需要這樣的東西:
$results = mysqli_query($db, "SELECT * from tbl_bugresponse WHERE bugid = $id ORDER BY time");
while ($posts = mysqli_fetch_assoc($results)) {
// table construction code goes here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.