Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created June 24, 2014 14:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sobstel/448fe080a52286a8e7ec to your computer and use it in GitHub Desktop.
Save sobstel/448fe080a52286a8e7ec to your computer and use it in GitHub Desktop.
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
Copy link

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