我正试图更新mysql表中5000多个视频的详细信息,但过了一段时间就会超时。
我基本上是循环浏览包含YouTube视频代码的记录,并使用YouTube提要API查找详细信息。
有人能帮我解释一下为什么以及如何修复它吗?
这是我的代码:-
<?php
$con = mysql_connect("hostname", "username", "pw");
if (!$con)
{
die('We could not connect you to our network at this moment. We apologise for the inconvenience please try again later');
}
mysql_select_db("sociallandscaper", $con);
set_time_limit(0);
$ccresult = mysql_query("SELECT vidcode FROM ytt");
while($row = mysql_fetch_array($ccresult)) {
$c = $c +1;
$vidcode = $row['vidcode'];
$url = "http://gdata.youtube.com/feeds/api/videos?q={$vidcode}&v=2&alt=json";
$json_single_vid = file_get_contents($url);
$json_single_vid_data = json_decode($json_single_vid);
$likes = $json_single_vid_data->{'feed'}->{'entry'}[0]->{'yt$rating'}->{'numLikes'};
$dislikes = $json_single_vid_data->{'feed'}->{'entry'}[0]->{'yt$rating'}->{'numDislikes'};
$raters = $json_single_vid_data->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'numRaters'};
$fav = $json_single_vid_data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'favoriteCount'};
$average = $json_single_vid_data->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'average'};
$comments = $json_single_vid_data->{'feed'}->{'entry'}[0]->{'gd$comments'}->{'gd$feedLink'}->{'countHint'};
$mSQL = "UPDATE ytt SET vidraters = '$raters',vidrating = '$average',vidlikes = '$likes',viddislikes = '$dislikes',vidcomments = '$comments',vidfavs = '$fav' WHERE vidcode = '$vidcode'";
mysql_query($mSQL);
echo $c."<br/>";
}
?>
如果有任何建议,我将不胜感激。
感谢
Jonathan
在更小的块中重构代码和进程。这有一个优势,那就是能够与现在可能只解决你问题的东西进行比较。我还可以补充一点,我不知道什么是"超时"到mysql的连接?整个剧本?连接到youtube?