Created
December 4, 2012 14:47
-
-
Save jtarleton/4204698 to your computer and use it in GitHub Desktop.
CSV Import Via PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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