Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created December 4, 2012 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtarleton/4204698 to your computer and use it in GitHub Desktop.
Save jtarleton/4204698 to your computer and use it in GitHub Desktop.
CSV Import Via PDO
<?php
require_once(dirname(__FILE__).'/model/DATABASE.class.php');
$adid = DATABASE::PDOCreate('mysql://root:123@localhost/db1');
$tablename = 'myawesometable';
$adid->exec(sprintf('DELETE from %s', $tablename));
$i=0;
if (($handle = fopen(dirname(__FILE__)."/xml/myawesomedata.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
echo $i.' ';
$stmt=$adid->prepare(
sprintf("INSERT INTO %s VALUES(:field,:field2,:field3)" , $tablename)
);
$stmt->bindValue(':field', $data[0]);
$stmt->bindValue(':field2', $data[1]);
$stmt->bindValue(':field3', $data[1]);
$stmt->execute();
$i++;
}
fclose($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment