【发布时间】:2016-08-24 15:27:31
【问题描述】:
我有一个包含一些字符串的大 txt 文件(40mb),文件是这样的:
AN,Aixas|AN,Aixirivall|AN,Aixovall|AN,Andorra la Vella|AN,Ansalonga|AN,Anyos|AN,Arans|
我有一个键(两个字母)和一个值(城市),所以当我尝试使用此函数在 PHP 中拆分它以及尝试在我的数据库中插入数据时,我遇到了问题。
我在php中使用的函数是:
$file = explode("|", file_get_contents('city.txt'));
$city = explode(",", $file);
foreach ($city as $key => $value)
mysqli_query($mysqli, "INSERT INTO city(name_city, id_city) VALUES ('$key', '$value')");
发生错误:
[29-Apr-2016 14:33:09 Europe/Berlin] PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in /home/mypath on line 6
我认为我的文件要处理的更大,我如何在我的数据库中解析和插入我的数据?
【问题讨论】:
-
使用 LOAD DATA INFILE dev.mysql.com/doc/refman/5.7/en/load-data.html
-
或者您可以使用插入数据创建文件dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html