Forum Moderators: coopster

Message Too Old, No Replies

Sleep in a while loop

         

Stu_Rogers

4:11 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



I'm trying to simulate a slow loading web page using the sleep() function.

I thought I could just drop this in my while loop but its not working as expected.

Instead of outputting one row at a time, its generating all the output after the loop has finished.

$result = @mysql_query("SELECT * FROM emails");
while ($record=mysql_fetch_array($result)) {
echo $record["email"];
sleep(3);
}

For example, if the loop returns 10 records, I wanted to echo each one with a 3 second pause between each.

But in practice the script is pausing 30 seconds, then outputting all 10 records.

phranque

4:17 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



it's probably buffering the output.
you have to flush [php.net]...

Stu_Rogers

4:32 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



Simply fantastic, I knew it must be something simple!

I just added flush() after sleep() and it works.

I didn't realise PHP buffers the output.

Thank you for your prompt reply.