内存限制已耗尽

发布于 2024-10-01 17:38:27 字数 3179 浏览 3 评论 0原文

在运行将大数据导入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”没有被查询解析​​。我不确定它是否未被识别,因为它以 * 开头。

While running a PHP script that imports large data into mysql, I encountered the following error:

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

I have before now, assigned a memory limit of 256M in my PHP INI file. This memory has so far been sufficient for my sripts and I have experienced no issues till now. I have imported files of size 1MB and 2.84MB while keeping the memory limit at 256MB. THis error occured when I runned a text file of about 1MB.

I have assumed the memory the error is refering to is the PHP memory limit. Could there be any other memory limits to be set? I would appreciate some suggestions in resolving this problem. Many thanks.

New edits

...
...
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);

I do not wish to start a new question since it is still on this issue.
I have resovled the " memory exhaustion" issue but now have a a mySQL query error has made my life quite miserable and I need help.

This is a sample of my input data from a text file:

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

This is the result from 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

I have come to notice that, the last variable "*67" from the input data is not being parsed by the query. I am not sure if it is not being recognised since it begins with *.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-10-08 17:38:27

一次插入一行还是批量插入,比如一次插入 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));

Do the inserts one row at a time or batch them into, say 50 rows at a time...

$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));
失退 2024-10-08 17:38:27

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

当我之前遇到此问题且无法增加内存限制时,我必须重写代码,以便不会同时将如此多的对象存储在内存中。你能发布一些代码吗?

There are other limits you can set in php.ini like max post size, max upload size, etc.... But that error looks like it's due to something else. Are you constructing really large objects [in php, most likely arrays] after you read the uploaded file ? I had a similar issue last night and all I had to do was increase my memory limit in php.ini.

When I've had this issue previously with no option to increase the memory limit, I had to rewrite my code so that so many objects were not stored in memory at the same time. Can you post some code ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文