内存限制耗尽

时间:2010-11-11 12:26:04

标签: php mysql memory-limit

在运行将大数据导入mysql的PHP脚本时,遇到以下错误:

memory exhausted near ''$GPGGA','130735.000','60.15751','N','24.74303','E','1','05','1.6','8.7','M','35' at line 1

我之前有过,在我的PHP INI文件中分配了256M的内存限制。到目前为止,这种记忆已经足够我的信号,直到现在我都没有遇到任何问题。我导入了大小为1MB和2.84MB的文件,同时将内存限制保持在256MB。当我运行大约1MB的文本文件时发生错误。

我假设错误所引用的内存是PHP内存限制。可以设置任何其他内存限制吗?我会很感激解决这个问题的一些建议。非常感谢。

新修改

...
...
if (is_array($inFile)) {

foreach($inFile as $inFileName) {

    $newFilePath = $upload_directory."/".$inFileName;

    if($inFileName != "")

    $handle = fopen($newFilePath,"r");
    $numlines =1;

    $query = "INSERT INTO gga_raw_data(device_id, gga_date,  nmea, gga_time, latitude, north, longitude, east, fixed_quality, no_of_satelites, hdop, altitude, meters, height_of_geoid, metres, check_sum, test_case_id) values";
    while (($data = fgetcsv($handle)) !== FALSE)
    {
        $numlines++;

        $myDay=substr($data[1], 0,2);
        $myMonth=substr($data[1], 2,2);
        $myYear=substr($data[1], 4,2);

        $gga_date = convertToNiceDate($myDay, $myMonth, 2000+$myYear);

        $longitude = convertToDegrees($data[6]);
        $latitude  = convertToDegrees($data[4]);


        if (!isset($data[16])) {
            $data[16] = '';
        }

        $query .="('$data[0]','$gga_date','$data[2]','$data[3]','$latitude','$data[5]','$longitude','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]', '$data[15]', '$data[16])',";


    }

    $query = substr($query, 0,-1);

我不想开始一个新问题,因为它仍然在这个问题上。 我已经解决了“内存耗尽”问题,但现在有一个mySQL查询错误让我的生活变得非常悲惨,我需要帮助。

这是我从文本文件中输入数据的示例:

1,070610,$GPGGA,080323.460,6013.45368,N,02445.28396,E,1,04,4.6,18.3,M,35.0,M,,*67
1,070610,$GPGGA,080327.000,6013.44424,N,02445.31214,E,1,05,1.9,30.8,M,35.0,M,,*66

这是echo $ query:

的结果
INSERT INTO gga_raw_data(device_id, gga_date, nmea, gga_time, latitude, north, longitude, east, fixed_quality, no_of_satelites, hdop, altitude, meters, height_of_geoid, metres,check_sum) values('1','2010,06,07','$GPGGA','080323.460','60.22423','N','24.75473','E','1','04','4.6','18.3','M','35.0','M','')('1','2010,06,07','$GPGGA','080327.000','60.22407','N','24.7552','E','1','05','1.9','30.8','M','35.0','M','')
)('1','2010,06,07','$GPGGA','053416.000','0','','0','','0','','','','','','','')('1','2010,06,07','$GPGGA','053431.000','0','','0','','0','','','','','','','')('1','2010,06,07','$GPGGA','062929.000','0','','0','','0','','','','','','','')('1','2010,06,07','$GPGGA','080250.000','0','','0','','0','','','','','','','')('1','2010,06,07','$GPGGA','080305.000','0','','0','','0','','','','','','','')('1','2010,06,07','$GPGGA','080305.000','0','','0','','0','','','','','','','')('1','2010,06,07','$GPGGA','080320.000','0','','0','','0','','','','','','','')


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('1','2010,06,07','$GPGGA','080327.000','60.22407','N','24.7552','E','1','05','1' at line 1

我注意到,查询没有解析输入数据中的最后一个变量“* 67”。我不确定它是否被识别,因为它始于*。

2 个答案:

答案 0 :(得分:2)

一次插入一行或将它们分批插入,例如每次50行......

$gen_query=$query; // keep $query as the stub

while (($data = fgetcsv($handle)) !== FALSE)
{
    $numlines++;

    ...
    $gen_query .="('$data[0]','$gga_date','$data[2]','$data[3]','$latitude','$data[5]','$longitude','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]', '$data[15]', '$data[16])',";
   if ($numlines % 50 == 0) {
      mysql_query(substr($gen_query,0,-1));
      $gen_query=$query;
   }
}
if ($gen_query!=$query) mysql_query(substr($gen_query,0,-1));

答案 1 :(得分:0)

你可以在php.ini中设置其他限制,例如最大帖子大小,最大上传大小等等。但是这个错误看起来像是由于其他原因。在阅读上传的文件后,你是在构建真正大的对象[在php中,很可能是数组]吗?昨晚我遇到了类似的问题,我所要做的就是增加php.ini中的内存限制。

当我之前遇到此问题而没有增加内存限制的选项时,我不得不重写我的代码,以便这么多对象不会同时存储在内存中。你能发一些代码吗?