Import large csv file to mysql database using php

后端 未结 2 356
陌清茗
陌清茗 2020-12-30 12:21

I have a very large CSV file (150 MB). What is the best way to import it to MySQL? I have to do some manipulation in PHP before inserting it into the MySQL table.

2条回答
  •  孤城傲影
    2020-12-30 12:54

    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)
    

提交回复
热议问题