title: How to reduce execution time?
body: <p>I have a facebook app where users can bet on a basketball game (guess the winner, the difference, the mvp and the top scorer).</p>
  
  <p>I keep one table with the games, one with the IDs of the users and one with the bets.</p>
  
  <p>If you guess the winner right, you get 1 point. If you guess the difference right, you get 5 points. If you guess the mvp right you get 3 points, and the same goes with the top scorer.</p>
  
  <p>Now I have a file that makes a table with all the users and their points:</p>
  
  <pre><code>        &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;
                  Name
              &lt;/td&gt;
              &lt;td&gt;
                  Points
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php
              $selectusers = $db-&gt;query("SELECT * FROM `predictorusers`");
              $c = 0;
              while ($appuser = $db-&gt;fetchRows($selectusers))
              {
                  try
                  {
                      $profile = $facebook-&gt;api('/'.$appuser['userid']);
                  } catch (FacebookApiException $e) {
                      continue;
                  }
                  $c++;
  
                  $points = 0;
  
                  $selectbets = $db-&gt;query("SELECT * FROM `predictor` WHERE `userid`='{$profile['id']}'");
                  while ($bet = $db-&gt;fetchRows($selectbets))
                  {
                      $selectgame = $db-&gt;query("SELECT * FROM `schedule` WHERE `id`='{$bet['gameid']}'");
                      $game = $db-&gt;fetchRows($selectgame);
  
                      if ($bet['winner'] == 1 &amp;&amp; $game['homescore'] &gt; $game['awayscore'])
                      {
                          $points = $points + 1;
  
                          if (($game['homescore'] - $game['awayscore']) == $bet['diff'])
                              $points = $points + 5;
                      }
                      elseif ($bet['winner'] == 2 &amp;&amp; $game['awayscore'] &gt; $game['homescore'])
                      {
                          $points = $points + 1;
  
                          if (($game['awayscore'] - $game['homescore']) == $bet['diff'])
                              $points = $points + 5;
                      }
  
                      $selectmvprkg = $db-&gt;query("SELECT MAX(`rkg`) FROM `boxscore` WHERE `game`='{$game['id']}'");
                      $mvprgk = $db-&gt;fetchRows($selectmvprkg);
  
                      $selectmvp = $db-&gt;query("SELECT * FROM `boxscore` WHERE `rkg`='{$mvprkg['rkg']}'");
                      while ($mvp = $db-&gt;countRows($selectmvp))
                      {
                          if ($mvp['player'] == $bet['mvp'])
                              $points = $points + 3;
                      }
  
                      $selecttopscorerpts = $db-&gt;query("SELECT MAX(`pts`) FROM `boxscore` WHERE `game`='{$game['id']}'");
                      $topscorerpts = $db-&gt;fetchRows($selecttopscorerpts);
  
                      $selecttopscorer = $db-&gt;query("SELECT * FROM `boxscore` WHERE `pts`='{$topscorerpts['pts']}'");
                      while ($topscorer = $db-&gt;fetchRows($selecttopscorer))
                      {
                          if ($topscorer['player'] == $bet['topscorer'])
                              $points = $points + 5;
                      }
                  }
          ?&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;?php echo $c; ?&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;?php echo $profile['name']; ?&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;?php echo $points; ?&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php
              }
          ?&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p>The only problem, is that the script takes more than 30 seconds to be fully executed, so the server stops it's execution and sends an erro:</p>
  
  <blockquote>
    <p>Fatal error: Maximum execution time of 30 seconds exceeded in <em>*</em> on line 37</p>
  </blockquote>
  
  <p>Does anyone know how can I prevent this from happening?</p>
  
  <p>Edit:</p>
  
  <p>Take a look on this file that contains 0 api calls:</p>
  
  <pre><code>        &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;
                  Name
              &lt;/td&gt;
              &lt;td&gt;
                  Points
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php
              $selectusers = $db-&gt;query("SELECT * FROM `predictorusers`");
              $c = 0;
              while ($appuser = $db-&gt;fetchRows($selectusers))
              {
                  $c++;
  
                  $points = 0;
  
                  $selectbets = $db-&gt;query("SELECT * FROM `predictor` WHERE `userid`='{$appuser['userid']}'");
                  while ($bet = $db-&gt;fetchRows($selectbets))
                  {
                      $selectgame = $db-&gt;query("SELECT * FROM `schedule` WHERE `id`='{$bet['gameid']}'");
                      $game = $db-&gt;fetchRows($selectgame);
  
                      if ($bet['winner'] == 1 &amp;&amp; $game['homescore'] &gt; $game['awayscore'])
                      {
                          $points = $points + 1;
  
                          if (($game['homescore'] - $game['awayscore']) == $bet['diff'])
                              $points = $points + 5;
                      }
                      elseif ($bet['winner'] == 2 &amp;&amp; $game['awayscore'] &gt; $game['homescore'])
                      {
                          $points = $points + 1;
  
                          if (($game['awayscore'] - $game['homescore']) == $bet['diff'])
                              $points = $points + 5;
                      }
  
                      $selectmvprkg = $db-&gt;query("SELECT MAX(`rkg`) FROM `boxscore` WHERE `game`='{$game['id']}'");
                      $mvprgk = $db-&gt;fetchRows($selectmvprkg);
  
                      $selectmvp = $db-&gt;query("SELECT * FROM `boxscore` WHERE `rkg`='{$mvprkg['rkg']}'");
                      while ($mvp = $db-&gt;countRows($selectmvp))
                      {
                          if ($mvp['player'] == $bet['mvp'])
                              $points = $points + 3;
                      }
  
                      $selecttopscorerpts = $db-&gt;query("SELECT MAX(`pts`) FROM `boxscore` WHERE `game`='{$game['id']}'");
                      $topscorerpts = $db-&gt;fetchRows($selecttopscorerpts);
  
                      $selecttopscorer = $db-&gt;query("SELECT * FROM `boxscore` WHERE `pts`='{$topscorerpts['pts']}'");
                      while ($topscorer = $db-&gt;fetchRows($selecttopscorer))
                      {
                          if ($topscorer['player'] == $bet['topscorer'])
                              $points = $points + 5;
                      }
                  }
          ?&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;?php echo $c; ?&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;?php echo $app['userid']; ?&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;?php echo $points; ?&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;?php
              }
          ?&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p>The same thing happens.</p>