doukongpao0903 2017-06-13 14:27
浏览 454
已采纳

php - 致命错误:允许的内存大小为134217728字节耗尽[重复]

I'm creating a function that imports a csv file into a database. But an error has occurred, and i was wondering how to fix this.

Error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

My function:

importFile("ingram", "ingram_fees.txt", "ingram_fees");

function importFile($company, $fileName, $tableName) {
    $filePath = "./ftp_imports/".$company."/".$fileName;
    $file = fopen($filePath, "r");
    $firstRowNames = fgetcsv($file);
    $columnNames = array();
    $rows = count($firstRowNames);
    $i = 0;
    while ($i < $rows) {
        array_push($columnNames, toCamelCase($firstRowNames[$i]));
    }
    if ($result = $mysqli->query("SHOW TABLES LIKE '".$tableName."'")) {
        if($result->num_rows !== 1) {
            $queryCreateTable = "CREAT TABLE $tableName (";
            $num = count($columnNames);
            $i = 0;
            while ($i < $num) {
                switch (gettype($columnNames[$i])) {
                    case 'string':
                        $queryCreateTable .= $columnNames[$i] . " VARCHAR(25)";
                        break;
                    case 'integer':
                        $queryCreateTable .= $columnNames[$i] . " DECIMAL(10,2)";
                        break;
                    case 'double':
                        $queryCreateTable .= $columnNames[$i] . " DECIMAL(10,2)";
                        break;
                    case 'NULL':
                        $queryCreateTable .= $columnNames[$i] . " VARCHAR(25)";
                        break;
                    default:
                        break;
                }
                if ($i !== ($num - 1)) {
                    $queryCreateTable .= ",";
                } else {
                    $queryCreateTable .= ");";
                }
            }
        }
    } else {//table already exists
        $queryDelAll = "TRUNCATE TABLE $tableName";
        $db->query($queryDelAll);

        $queryImport = <<<eof
            LOAD DATA LOCAL INFILE '$filePath'
            INTO TABLE $tableName
            FIELDS TERMINTED BY ','
            LINES TERMINATED BY '
'
            INGORE 1 LINES
            ($columnNames)
            eof;
            $db->query($queryImport);
        }
    }

and the function that gives the error because of its memory usage.

function toCamelCase($string, $capitalizeFirstCharacter = false) {
    $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
    if (!$capitalizeFirstCharacter) {
        $str[0] = strtolower($str[0]);
    }
    return $str;
}

Because the $columnNames array is only 8 values long I don't know why the memory usage is that high.

Can someone help me out?

Thanks, Per

</div>
  • 写回答

1条回答 默认 最新

  • drwjv28028 2017-06-13 14:32
    关注

    There is an infinite loop because you forgot to increment $i.

    $i = 0;
    while ($i < $rows) {
        array_push($columnNames, toCamelCase($firstRowNames[$i]));
    }
    

    better:

    $i = 0;
    while ($i < $rows) {
        array_push($columnNames, toCamelCase($firstRowNames[$i]));
        ++$i; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?