简体   繁体   English

停止连续表生成

[英]Stop continuous table generation

I am trying to stop the data in the following table from continuing to repeat itself, as in looping on the same data, crashing the browser: 我试图阻止下表中的数据继续重复自身,就像在循环访问相同数据时一样,导致浏览器崩溃:

Here's the query: 这是查询:

$posts = mysqli_fetch_assoc(mysqli_query($db, "SELECT * from tbl_bugresponse WHERE bugid = $id ORDER BY time")); 

And here's the table code: 这是表代码:

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

Note that I am using mysqli not mysql. 请注意,我使用的是mysqli而不是mysql。

The problem is the while($posts){ line. 问题是while($posts){行。

You are writing code which says "Keep going until someone sets the posts variable to null or false", but then you never touch it... so it will keep going forever. 您正在编写代码,上面写着“继续前进,直到有人将posts变量设置为null或false为止”,但您再也不会碰它……因此它将永远持续下去。

Update 更新资料

Had another look at your query, it looks like you've got confused about how it's supposed to work. 再次查看您的查询,您似乎对它应该如何工作感到困惑。 Here's what it should look like: 它应该是这样的:

$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
}

You'll note that the mysqli_query call has been separated out. 您会注意到mysqli_query调用已被分​​离出来。 What mysqli_query does is run the database query, then give you a "handle" to the list of rows that come back from the query. mysqli_query作用是运行数据库查询,然后为您提供从查询返回的行列表的“句柄”。 This is common point of confusion as mostly people expect it to give them an array of all the rows, not a "handle". 这是常见的混淆点,因为大多数人希望它能为他们提供所有行的数组,而不是“句柄”。

You then have to call mysql_fetch_assoc to get the first row out of the list, do something with it, then call mysql_fetch_assoc again repeatedly to get the next row. 然后,您必须调用mysql_fetch_assoc从列表中取出第一行,对其进行处理,然后再次重复调用mysql_fetch_assoc以获取下一行。

When it runs out of rows, mysql_fetch_assoc will return null instead of a valid row, so that's how you can check to see when you've finished, and to stop the loop. 当它用完所有行时, mysql_fetch_assoc将返回null而不是有效行,因此您可以通过这种方式查看完成的时间并停止循环。

You aren't progressing through the result set. 您并没有完成结果集。 mysqli_query gets your resultset, and mysqli_fetch_assoc progresses through it. mysqli_query获取结果集,并且mysqli_fetch_assoc逐步进行搜索。 You need something like this: 您需要这样的东西:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM