Php – Best Practice : Import CSV to MYSQL Database using PHP 5.x

phpsql

what is the best solution to import large amounts of records into an MySQL or Oracle Database. I Think there are two ways :

  • Insert every record with an persistent connection
  • Create on BIG SQL-Statement and query the Database

Iam not sure wich is performing better ? Is there any Best Practice Solutions for this kind of operation ??

Best Solution

There is one other possibility: LOAD DATA INFILE

LOAD DATA LOCAL INFILE 'foobar.csv' 
INTO TABLE foobar
FIELDS TERMINATED BY ',' ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n';

This requires that the MySQL server has access to the physical file. But moving/copying a file to an import directory should not be hard with PHP.

Related Question