Import large csv file to mysql database using php

对着背影说爱祢 提交于 2019-11-30 07:23:54

You could take a look at LOAD DATA INFILE in MySQL.

You might be able to do the manipulations once the data is loaded into MySQL, rather than first reading it into PHP. First store the raw data in a temporary table using LOAD DATA INFILE, then transform the data to the target table using a statement like the following:

INSERT INTO targettable (x, y, z)
SELECT foo(x), bar(y), z
FROM temptable

I would just open it with fopen and use fgetcsv to read each line into an array. pseudo-php follows:

mysql_connect( //connect to db);

$filehandle = fopen("/path/to/file.csv", "r");
while (($data = fgetcsv($filehandle, 1000, ",")) !== FALSE) {
    // $data is an array
    // do your parsing here and insert into table
}

fclose($filehandle)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!