Parsing CSV files into a MySQL Table
| From: | Mark Benson | Date: | Tue, 02 Nov 2004 13:46:36 +0000 |
| Subject: | Parsing CSV files into a MySQL Table | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-37139@lists.php.net to get a copy of this message | ||
Hi, newbie here, go easy on me I'm learning this as I go ;)
Got a bit of teaser here.
//<?php
mysql_connect (localhost, foouser, foologin);
mysql_select_db (footest1);
$csvfile = file("http://foo.com/foolist.csv");
foreach($csvfile as $line_no => $datastring) {
$data = explode (",", $datastring);
$foo1 = $data[0];
$foo2 = $data[1];
$foo3 = $data[2];
$foo4 = $data[3];
$foo5 = $data[4];
$foo6 = $data[5];
$foo7 = $data[6];
mysql_query ("INSERT INTO 'footable' (foo1, foo2, foo3, foo4, foo5, foo6, foo7)
VALUES '$foo1', '$foo2', '$foo3', '$foo4',
'$foo5', '$foo6', '$foo7')");
}
//?>
The result of the above is I get nothing INSERT-ed in 'footable'. No lines of data at all.
I looked in the table using phpMyAdmin and zilch.
I have however dumped the contents of each variable in the 'foreach' loop to the screen in
a table and it all maps out correctly as i was in the CSV file, so the CSV file is being parsed
correctly. The fault seems to be in the MySQL query I think.
Privilages have no influence - I've tried both the database's 'regular' users
and also the 'root' user and it makes no odds.
I've also tried dumping the list of single variables and using '($data[0], $data[1]
etc...) and that has no effect.
It's probably something glaringly obvious to an expert but as I say I'm learning as I go
so any help would be great :)
Thx.
--
Mark Benson
http://homepage.mac.com/markbenson