title: PHP timeouts while connecting to mysql
body: <p>I am developing chat application in PHP + Mysql.</p>
  
  <p>My idea was that I open script in background(ajax) which will keep loading for a minute to check if user got any new message.</p>
  
  <p>Here it is</p>
  
  <pre><code>    $start = time();
  
      while(time() - $start &lt; 60)
      {
  
      $result = mysql_query("SELECT * FROM messages WHERE `to` = '$me_user'");
  
          if(mysql_num_rows($result))
          {
          $row = mysql_fetch_array($result);
          $id = $row['id'];
          $from = $row['from'];
          $time = $row['time'];
          $msg = $row['message'];
  
          mysql_query("DELETE FROM messages WHERE id = $id");
  
          die("appendMsg('$msg',to,'$time');");
          }
      $result = null;
      Sleep(2);
      }
  </code></pre>
  
  <p>So actually it checks for new message every 2 seconds.</p>
  
  <p>But my problem is that whole website starts "lagging" because of this.
  I think problem is in Sleep().</p>
  
  <p>Sometimes even PHP timeouts while trying to connect to DB.</p>
  
  <blockquote>
    <p>Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\chatly\config.php on line 19</p>
  </blockquote>
  
  <p>Any suggestion how could I pause for 2 seconds?
  Or you think it would be better to send new request each 2 seconds via ajax?</p>