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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题