【问题标题】:php - Fatal error: Allowed memory size of 134217728 bytes exhausted [duplicate]php - 致命错误:允许的内存大小为 134217728 字节已用尽 [重复]
【发布时间】:2017-11-15 09:37:54
【问题描述】:

我正在创建一个将 csv 文件导入数据库的函数。但是发生了错误,我想知道如何解决这个问题。

错误: 致命错误:允许的内存大小为 134217728 字节已用尽(尝试分配 32 字节)

我的功能:

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 '\n'
            INGORE 1 LINES
            ($columnNames)
            eof;
            $db->query($queryImport);
        }
    }

以及由于内存使用而导致错误的函数。

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

因为$columnNames数组只有8个值,我不知道为什么内存使用率那么高。

谁能帮帮我?

谢谢, 每

【问题讨论】:

  • 通过编辑正在使用的php.ini 文件或设置ini_set("memory_limit", "...");(例如256M 为256 Mb)来提高您的php 安装的内存限制
  • 你需要更新php ini memory_limit
  • $i 在你的循环中永远不会增加,因此$columnNames 会无限增长。
  • @Blackhole 哎呀,这是一个愚蠢的错误,没有看到。感谢您的关注

标签: php


【解决方案1】:

因为忘记增加 $i,所以出现了无限循环。

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

更好:

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

【讨论】:

  • 愚蠢的错误。感谢您的帮助:)
猜你喜欢
  • 2015-07-06
  • 2011-02-23
  • 2016-04-13
  • 2016-12-11
  • 2015-11-16
  • 2016-07-12
  • 2017-01-05
  • 2017-08-26
  • 2012-09-26
相关资源
最近更新 更多