Created
October 25, 2013 17:07
-
-
Save cam8001/7158153 to your computer and use it in GitHub Desktop.
Back of the envelope PHP script to import CSV for Rohan.
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
#!/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