我在更新mysql数据库时遇到问题。 我想在一个~800,000行的数据库上运行这个脚本,但是我的内存在5行之后耗尽,所以我需要通过向LIMIT添加+5来自动更新这个mysql查询。
我想使用重定向或以某种方式重置内存..
<?php
include(dirname(__FILE__) .'/include/simple_html_dom.php');
mysql_connect('localhost', '', '');
mysql_select_db('');
$result = mysql_query("SELECT gamertag FROM gamertags LIMIT 0, 5 ");
while ($row = mysql_fetch_assoc($result)) {
$gamertag = $row['gamertag'];
//pages to pull stats from
$site_url = 'http://www.bungie.net';
$stats_page = 'http://www.bungie.net/stats/reach/default.aspx?player=';
//create dom
$html = new simple_html_dom();
$html->load_file($stats_page . urlencode(strtolower($gamertag)));
//pull nameplate emblem url, ****if it exist****
$nameplate_emblem_el = $html->find("#ctl00_mainContent_identityBar_namePlateImg");
if (!$nameplate_emblem_el){
$nameplate_emblem = 'No nameplate emblem!';
}
else{
//only if #ctl00_mainContent_identityBar_namePlateImg is found
$nameplate_emblem = htmlspecialchars_decode($nameplate_emblem_el[0]->attr['src']);
$nameplate_emblem = $site_url . $nameplate_emblem;
}
mysql_query("INSERT IGNORE INTO star SET gamertag = '".$gamertag."',nameplate = '".$nameplate_emblem."'");
}
?>
答案 0 :(得分:0)
800.000是一个很大的数字,不确定它是否会发生,但来自对php手册的评论: “unset()就像它的名字所说的那样 - 取消设置一个变量。它不会强制立即释放内存.PHP的垃圾收集器会在看到拟合时执行它 - 尽快意图,因为无论如何都不需要那些CPU周期,或者在脚本耗尽内存之前,无论先发生什么。
如果你正在做$ whatever = null;那么你正在重写变量的数据。您可能会更快地释放/缩小内存,但它可能会更快地从真正需要它们的代码中窃取CPU周期,从而导致更长的总执行时间。“
出处:http://php.net/manual/en/function.unset.php
因此,每次循环运行后,尝试取消设置/设置为空变量。
答案 1 :(得分:0)
我猜你已经达到了你的php内存限制,而不是机器。如果是这样,你可以编辑php.ini(通常是/etc/php5/apache2/php.ini)来为php提供更多内存。查找“memory_limit = X”,其中X类似于256M