Skip to content

Instantly share code, notes, and snippets.

@pkdavies
Created July 8, 2011 11:51
Show Gist options
  • Save pkdavies/1071669 to your computer and use it in GitHub Desktop.
Save pkdavies/1071669 to your computer and use it in GitHub Desktop.
fgetcsv to mysql insert statements
<pre>
<?php
$row = 0;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$model_id = $data[1];
for ($c=2; $c < $num; $c++) {
if (!empty($data[$c])){
echo "INSERT INTO `test_report_fields_to_model` (`test_report_fields_id`, `model_id`) VALUES ($data[$c], $model_id);\n";
}
}
$row++;
}
fclose($handle);
}
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment