When ever I had to import a CSV into database table, i've always written my own csv parser / importer. It's quite simple.
Here's an example.
test.csv
Firstname,Lastname,Age
"Latheesan","Kanes",26
"Adam","Smith",30
test.php
<?php
// Mini Config
$csv_file = 'test.csv';
$delimiter = ',';
$enclosure = '"';
$skip_first_row = true;
$import_chunk = 250;
// Parse CSV & Build Import Query
$import_queries = array();
$first_row_skipped = false;
if (($handle = fopen($csv_file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter, )) !== FALSE) {
if ($skip_first_row && !$first_row_skipped) {
$first_row_skipped = true;
continue;
}
list($firstname, $lastname, $age) = $data;
$import_queries[] = "INSERT INTO myTable (firstname, lastname, age) VALUES ('$firstname', '$lastname', $age);";
}
fclose($handle);
}
// Proceed if any data got parsed
if (sizeof($import_queries))
{
foreach(array_chunk($import_queries, $import_chunk) as $queries)
{
$dbh->query(implode(' ', $queries));
}
}
?>
The parsed queries will look like this (if you print_r it):
Array
(
[0] => INSERT INTO myTable (firstname, lastname, age) VALUES ('Latheesan', 'Kanes', 26);
[1] => INSERT INTO myTable (firstname, lastname, age) VALUES ('Adam', 'Smith', 30);
)
You have two option for the actual importing into the db:
Build a collection of import sql query and execute it in a batch (array_chunk) - this means less queries against your db. However as you can see, im not checking the values from the CSV - i.e. i trust my data source and not escaping anything - a bit dangerous...
You execute the query, as soon as you built it with escaping the values - small drawback is that it will execute one query per row in csv....
Hope this helps.