我有一个 Facebook 应用程序,用户可以在其中对篮球比赛进行投注(猜猜获胜者、差异、mvp 和最佳得分手)。
我在一张 table 上放游戏,一张 table 上有用户的 ID,一张 table 上有赌注。
如果您猜对了获胜者,您将获得 1 分。如果您猜对了差异,您将获得 5 分。如果您猜对了 mvp,您将获得 3 分,最高得分手也是如此。
现在我有一个文件,其中包含所有用户及其积分:
<table>
<tr>
<td></td>
<td>
Name
</td>
<td>
Points
</td>
</tr>
<?php
$selectusers = $db->query("SELECT * FROM `predictorusers`");
$c = 0;
while ($appuser = $db->fetchRows($selectusers))
{
try
{
$profile = $facebook->api('/'.$appuser['userid']);
} catch (FacebookApiException $e) {
continue;
}
$c++;
$points = 0;
$selectbets = $db->query("SELECT * FROM `predictor` WHERE `userid`='{$profile['id']}'");
while ($bet = $db->fetchRows($selectbets))
{
$selectgame = $db->query("SELECT * FROM `schedule` WHERE `id`='{$bet['gameid']}'");
$game = $db->fetchRows($selectgame);
if ($bet['winner'] == 1 && $game['homescore'] > $game['awayscore'])
{
$points = $points + 1;
if (($game['homescore'] - $game['awayscore']) == $bet['diff'])
$points = $points + 5;
}
elseif ($bet['winner'] == 2 && $game['awayscore'] > $game['homescore'])
{
$points = $points + 1;
if (($game['awayscore'] - $game['homescore']) == $bet['diff'])
$points = $points + 5;
}
$selectmvprkg = $db->query("SELECT MAX(`rkg`) FROM `boxscore` WHERE `game`='{$game['id']}'");
$mvprgk = $db->fetchRows($selectmvprkg);
$selectmvp = $db->query("SELECT * FROM `boxscore` WHERE `rkg`='{$mvprkg['rkg']}'");
while ($mvp = $db->countRows($selectmvp))
{
if ($mvp['player'] == $bet['mvp'])
$points = $points + 3;
}
$selecttopscorerpts = $db->query("SELECT MAX(`pts`) FROM `boxscore` WHERE `game`='{$game['id']}'");
$topscorerpts = $db->fetchRows($selecttopscorerpts);
$selecttopscorer = $db->query("SELECT * FROM `boxscore` WHERE `pts`='{$topscorerpts['pts']}'");
while ($topscorer = $db->fetchRows($selecttopscorer))
{
if ($topscorer['player'] == $bet['topscorer'])
$points = $points + 5;
}
}
?>
<tr>
<td>
<?php echo $c; ?>
</td>
<td>
<?php echo $profile['name']; ?>
</td>
<td>
<?php echo $points; ?>
</td>
</tr>
<?php
}
?>
</table>
唯一的问题是脚本需要超过 30 秒才能完全执行,因此服务器停止执行并发送错误:
Fatal error: Maximum execution time of 30 seconds exceeded in * on line 37
有谁知道如何防止这种情况发生吗?
编辑:
看看这个包含 0 个 API 调用的文件:
<table>
<tr>
<td></td>
<td>
Name
</td>
<td>
Points
</td>
</tr>
<?php
$selectusers = $db->query("SELECT * FROM `predictorusers`");
$c = 0;
while ($appuser = $db->fetchRows($selectusers))
{
$c++;
$points = 0;
$selectbets = $db->query("SELECT * FROM `predictor` WHERE `userid`='{$appuser['userid']}'");
while ($bet = $db->fetchRows($selectbets))
{
$selectgame = $db->query("SELECT * FROM `schedule` WHERE `id`='{$bet['gameid']}'");
$game = $db->fetchRows($selectgame);
if ($bet['winner'] == 1 && $game['homescore'] > $game['awayscore'])
{
$points = $points + 1;
if (($game['homescore'] - $game['awayscore']) == $bet['diff'])
$points = $points + 5;
}
elseif ($bet['winner'] == 2 && $game['awayscore'] > $game['homescore'])
{
$points = $points + 1;
if (($game['awayscore'] - $game['homescore']) == $bet['diff'])
$points = $points + 5;
}
$selectmvprkg = $db->query("SELECT MAX(`rkg`) FROM `boxscore` WHERE `game`='{$game['id']}'");
$mvprgk = $db->fetchRows($selectmvprkg);
$selectmvp = $db->query("SELECT * FROM `boxscore` WHERE `rkg`='{$mvprkg['rkg']}'");
while ($mvp = $db->countRows($selectmvp))
{
if ($mvp['player'] == $bet['mvp'])
$points = $points + 3;
}
$selecttopscorerpts = $db->query("SELECT MAX(`pts`) FROM `boxscore` WHERE `game`='{$game['id']}'");
$topscorerpts = $db->fetchRows($selecttopscorerpts);
$selecttopscorer = $db->query("SELECT * FROM `boxscore` WHERE `pts`='{$topscorerpts['pts']}'");
while ($topscorer = $db->fetchRows($selecttopscorer))
{
if ($topscorer['player'] == $bet['topscorer'])
$points = $points + 5;
}
}
?>
<tr>
<td>
<?php echo $c; ?>
</td>
<td>
<?php echo $app['userid']; ?>
</td>
<td>
<?php echo $points; ?>
</td>
</tr>
<?php
}
?>
</table>
同样的事情也会发生。
最佳答案
我建议以某种方式缓存 API 调用的结果,这样您就不必在每次页面加载时为每个用户调用 API。
您可能还想查看Facebook API functions that allow you to request data for more than a single user at a time - 对结果集中的所有用户进行一次 API 调用比对每个用户进行单独的 API 调用要快得多。
此外,一般来说,在多个级别的循环中嵌套查询并不是一个好主意 - 尝试减少正在运行的查询总数。
关于php - 如何减少执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7623713/