我有这个 PHP 代码:
<?php
include_once("connect_to_mysql.php");
$max=300;
while($max--)
{
sleep(1);
doMyThings();
}
?>
它应该重复 mysql 查询 300 次,每次之间的间隔为 1 秒。但问题是大约一分钟后,我在浏览器中收到此消息:未收到数据。无法加载网页,因为服务器没有发送数据。
问题如下:您的代码至少会(不考虑doMyThings()
)最后 300 秒。大多数 PHP 环境将默认脚本运行时间设置为大约 60 秒,脚本停止并且不打印任何内容。
接下来的事情是(如果脚本执行时间设置得足够高以允许长时间运行脚本),脚本必须运行直到完成(即约 300 秒),之后数据将写入输出流。在那之前,您不会看到任何输出。
要避免这两个问题,请参阅以下代码:
<?php
// If allowed, unlimited script execution time
set_time_limit(0);
// End output buffering
ob_end_flush();
include_once("connect_to_mysql.php");
$max=300;
// End output buffering IE and Safari Workaround
// They will only display the webpage if it's completely loaded or
// at least 5000 bytes have been "printed".
for($i=0;$i<5000;$i++)
{
echo ' ';
}
while($max > 0)
{
sleep(1);
doMyThings();
$max--;
// Manual output buffering
ob_flush();
flush();
}
?>
也许您也对这篇文章感兴趣:逐步输出exec() ping结果 https://stackoverflow.com/questions/10807607/outputting-exec-ping-result-progressively/10814087#10814087
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)