|
Posted by Martin Mouritzen on 11/01/06 19:10
On Wed, 01 Nov 2006 18:15:30 GMT, "Shooter" <SDshooter@hotmail.com>
wrote:
>I have a downloadable roster of members that I can get in a CSV format. Does
>anyone know of a script that can automatically update my mySQL database
>online with this file? Ideally I'd love to either be able to upload the CSV
>from a hidden page and have the script do the update, or else dump the file
>into a specific directory and have the server check for new files to import
>nightly. Any thoughts or recommendations?
Well, that depends if you know any PHP in advance? Breaking down CSV
and manipulating the contents is really easy.
Food for thoughts:
<?
// Path to CSV File
$csvfile = "mycsvfile.csv";
// Seperator in the CSV file (sometimes it's another seperator than
";")
$seperator = ";";
// Read the CSV file
$lines = file($csvfile);
// Loop each line
foreach($lines as $line) {
// Split each line at the seperator, creating an array with
the rows.
$rows = explode($seperator,$line);
// Do the query, please note that the rows will probably not
be the same
// as in this example, check your CSV file and make sure to
update
// the insert query to match the csv-fields with the fields in
mysql.
$query = "
INSERT INTO
members
SET
name = '".addslashes($row[0])."',
address = '".addslashes($row[1])."'
";
mysql_query($query,$connection) or
die("\n<br>\n<b>SQL-Error:</b> ".mysql_error()."<br>\n<b>SQL:</b>
".$query);
}
--
best regards,
Martin Mouritzen.
http://www.siteloom.dk
[Back to original message]
|