[英]PHP while with sleep loop not updating database until end of loop
I have a while loop with a sleep function that I wan´t to run -> update database -> sleep -> run again for a number of 10 times. 我有一个带有睡眠功能的while循环,我不想运行 - >更新数据库 - >睡眠 - >再次运行10次。 With the below script the script loops 10 times, but it only updates the database when all 10 loops have finished. 使用下面的脚本,脚本循环10次,但它只在所有10个循环完成时更新数据库。
I added an echo at the end just to verify, and the echo does not appear on the page until the and when every loop is echoed out at once. 我在末尾添加了一个回声来验证,并且直到每个循环立即被回显时,回显没有出现在页面上。
I have also tried placing ob_flush() and flush() after the echo with no luck. 我也尝试在回声之后放置ob_flush()和flush()而没有运气。
Script 脚本
$loops = 10;
while ($loops > 0)
{
while($row = mysql_fetch_array($result))
{
// Get current User status
$username = $row['username'];
$user_status = $row['user_status'];
$user_updated = date('Y-m-d H:i:s');
// Update database
$update_sql = "UPDATE `database`.`user` SET `user_status` = '$user_status',
`user_updated` = '$agent_updated'
WHERE
`user`.`username` = '$username'";
$update=mysql_query($update_sql);
echo "Loop #".$loops."<br>";
}
flush();
$loops--;
Sleep(5);
}
I've also tried the same with a FOR loop . 我也试过FOR循环 。
Any suggestions would be appreciated. 任何建议,将不胜感激。 I've gone through the search and haven´t found users with similar issues but I couldn't find the answer. 我已经完成了搜索,但没有找到类似问题的用户,但我找不到答案。
You can get those echoes to show one by one, but on a controlled environment (according to php documentation, flush may still buffer the output on several Win32 systems), you can read further in the php flush manual page . 您可以逐个显示这些回声,但在受控环境中(根据php文档,flush可能仍会缓冲几个Win32系统上的输出),您可以在php flush手册页中进一步阅读。
To be more specific, make sure APaches mod_gzip is disabled, also, some php.ini settings like output_buffering and zlib.output_compression should be set to false. 更具体地说,确保禁用APaches mod_gzip,同时,一些php.ini设置(如output_buffering和zlib.output_compression)应设置为false。
output_buffering = false
zlib.output_compression = false
Even with all of this, you may also need to send a "big" amount of data (like 2048 chars) in order to make some browser to actually display new data. 即使有这些,您可能还需要发送“大量”数据(如2048个字符),以使某些浏览器实际显示新数据。
echo "One"
echo str_repeat(" ", 2048);
Sleep(3)
echo "Two"
echo str_repeat(" ", 2048);
// Ans so on
In the comments of the aforementioned manual page, you can see more examples. 在上述手册页的注释中,您可以看到更多示例。
根据提供的代码,似乎所有更新都在第一个循环中完成,然后mysql_fetch_array($ result)仅返回falses并且内部循环不会在其他主循环中再次进行,除非您再次填充$ result(之间)而循环标题)。
As pointed out by NB
that PHP only returns results all at once after the script runs, I have ended with the following solution: 正如NB
指出的那样,PHP只在脚本运行后立即返回结果,我已经用以下解决方案结束了:
A bash.sh script that runs the PHP page every 5 seconds: 每5秒运行一次PHP页面的bash.sh脚本:
#!/bin/bash
set -e
count=1
while [ $count -le 12 ] :
do
# run this scripts for one minute (12 times every 5 seconds = 60 seconds)
/usr/bin/php -q thescript.php
(($count++))
sleep 5
done
Then a cronjob that runs the page every minute. 然后是每分钟运行页面的cronjob。 The above works and solves my issue. 以上工作并解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.