我将这个PHP代码连接到数据库并回显出电子邮件地址列表。问题是,即使我刷新了页面,它的更新速度也非常慢。我的第一个想法是浏览器缓存了信息并将其保留了一段时间,但我添加了这个:`
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />`
在我的head标签中阻止浏览器缓存,但它没有工作,列表仍然非常慢。任何帮助表示赞赏。 (对不起瑞典的一些变种。)
<?php
$servername = "***********";
$username = "***********";
$password = "***********";
$dbname = "***********";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT mejl FROM mejllista";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<li>" . $row["mejl"] . "</li>";
}
} else {
echo "0 resultat";
}
$conn->close();
?>
答案 0 :(得分:0)
因此,PHP会将循环中生成的输出(例如带有echo)存储到输出缓冲区。一旦缓冲区已满,它就会将其刷新到输出 - 您的网页。
要覆盖它,您可以隐式调用flush和ob_flush: http://php.net/manual/en/function.flush.php http://php.net/manual/en/function.ob-flush.php
<?php
$servername = "***********";
$username = "***********";
$password = "***********";
$dbname = "***********";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT mejl FROM mejllista";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<li>" . $row["mejl"] . "</li>";
//Flush the system write buffers
flush();
//Send the contents of the output buffer
ob_flush();
}
} else {
echo "0 resultat";
}
$conn->close();
?>
在while循环的每次迭代中,刷新写缓冲区并将缓冲区的内容发送到页面。
享受:)