title: PHP while with sleep loop not updating database until end of loop
body: <p>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. With the below script the script loops 10 times, but it only updates the database when all 10 loops have finished.</p>
  
  <p>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.</p>
  
  <p>I have also tried placing <strong>ob_flush()</strong> and <strong>flush()</strong> after the echo with no luck. </p>
  
  <p><strong>Script</strong></p>
  
  <pre><code>$loops = 10;
  while ($loops &gt; 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."&lt;br&gt;";
    }
  flush();
  $loops--;
  Sleep(5);
  }
  </code></pre>
  
  <p>I've also tried the same with a <strong>FOR loop</strong>.<br>
  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.</p>