You are here: Re: scripts to import a CSV into mySQL online? « All PHP « IT news, forums, messages
Re: scripts to import a CSV into mySQL online?

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация