Skip to content

Instantly share code, notes, and snippets.

@cam8001
Created October 25, 2013 17:07
Show Gist options
  • Save cam8001/7158153 to your computer and use it in GitHub Desktop.
Save cam8001/7158153 to your computer and use it in GitHub Desktop.
Back of the envelope PHP script to import CSV for Rohan.
#!/usr/bin/php
/**
* Imports a CSV file into a database.
*/
$file = $_ARGV[0];
$handle = fopen($file,"r");
$key = 0;
$query = "
INSERT INTO
`firstName`,
`lastName`,
`awardCategory`,
`tvOrFilmShow`,
`awardShow`,
`awardYear`,
`awardReceived`,
`id`,
(
%s,
)
");
// Loop through the csv file and insert into database.
do {
if ($data[0]) {
$data = array_map($data, 'mysqli_real_escape_string');
$data[] = $key;
$query_values = implode(',', $data);
mysql_query(sprintf($query, $query_values);
$key++;
}
} while ($data = fgetcsv($handle,1000,",","'"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment