Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert CSV to SQL INSERT statements
<?php
$file = $argv[1];
$table = $argv[2];
$ls = file($file);
$out = "";
foreach ($ls as $l) {
$d = str_getcsv(trim($l));
$q = sprintf('INSERT INTO `%s` VALUES ("%s");', $table, implode('","', $d)).PHP_EOL;
echo $q;
$out .= $q;
}
file_put_contents($table.'.sql', $out);
@richardkeep

This comment has been minimized.

richardkeep commented Feb 14, 2015

I want to skip or ignore the first line, which is the header, how do I go about that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment