PHP内存耗尽错误,代码不正确或只是增加了内存限制? [英] PHP Memory Exhaustion error, poor code or just increase memory limit?

查看:81
本文介绍了PHP内存耗尽错误,代码不正确或只是增加了内存限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将738627记录从平面文件读取到MySQl中.该脚本似乎可以正常运行,但是却给了我上面的内存错误.

I am trying to read 738627 records from a flat file into MySQl. The script appears to run fine, but is giving me the above memory errors.

该文件的示例为:

#export_dategenre_idapplication_idis_primary
#primaryKey:genre_idapplication_id
#dbTypes:BIGINTINTEGERINTEGERBOOLEAN
#exportMode:FULL
127667880285760002817317350
127667880285760002818261461
127667880285760002825372301
127667880285760002827785570
127667880285760002827930241
127667880285760002827987861
127667880285760002828089791
127667880285760002828168361
127667880285760002828192041
127667880285760002829144541
127667880285760002829351511

我尝试使用来增加允许的内存

I have tried increasing the allowed memory using

ini_set("memory_limit","80M");

,但仍然失败.我会不断增加直到运行吗?

and it still fails. Do I keep upping this until it runs?

完整的代码是

  <?php
    ini_set("memory_limit","80M");
    $db = mysql_connect("localhost", "uname", "pword");
    // test connection 
    if (!$db) { 
        echo "Couldn't make a connection!"; 
        exit; 
    } 
    // select database 
    if (!mysql_select_db("dbname",$db))

    {
        echo "Couldn't select database!"; 
        exit; 
    }
    mysql_set_charset('utf8',$db);

    $delimiter = chr(1);
    $eoldelimiter = chr(2) . "\n";
    $fp = fopen('genre_application','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

 $loop = 0;
while (!feof($fp)) {
  $loop++;
    $line = stream_get_line($fp,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue;  //Skip lines that start with # 
    $field[$loop] = explode ($delimiter, $line);
    $fp++;
$export_date = $field[$loop][0];
$genre_id = $field[$loop][1];
$application_id = $field[$loop][2];

$query = "REPLACE into genre_apps
(export_date, genre_id, application_id)
VALUES ('$export_date','$genre_id','$application_id')";
print "SQL-Query: ".$query."<br>";
       if(mysql_query($query,$db))
         {
          echo " OK !\n";
         }
        else
             {
              echo "Error<br><br>";
              echo mysql_errno() . ":" . mysql_error() . "</font></center><br>\n";
             }

    }

    fclose($fp);
    ?> 

推荐答案

您的循环无缘无故地填充变量$field(它在每次循环迭代中写入不同的单元格),从而每行占用更多的内存.

Your loop fills the variable $field for no reason (it writes to a different cell on every loop iteration), thereby using up more memory with every line.

您可以替换:

$field[$loop] = explode ($delimiter, $line);
$export_date = $field[$loop][0];
$genre_id = $field[$loop][1];
$application_id = $field[$loop][2];

使用:

list($export_date, $genre_id, $application_id) = explode($delimiter, $line);

为提高性能,可以通过将N行分组到单个查询中来利用使用REPLACE INTO插入多行的功能.

For improved performance, you could take advantage of the ability to insert several lines using REPLACE INTO by grouping N lines into a single query.

这篇关于PHP内存耗尽错误,代码不正确或只是增加了内存限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆