RE: [PHP-DB] Parsing CSV files into a MySQL Table

2004-11-02 Thread Alex Bovey
>   mysql_query ("INSERT INTO 'footable' (foo1, foo2, foo3, 
> foo4, foo5, foo6, foo7) 
>   VALUES '$foo1', '$foo2', '$foo3', '$foo4', 
> '$foo5', '$foo6', '$foo7')"); }

You're also missing an opening bracket after 'VALUES'.  The statement should
be:

mysql_query ("INSERT INTO 'footable' (foo1, foo2, foo3, foo4, foo5, foo6,
foo7) 
VALUES ('$foo1', '$foo2', '$foo3', '$foo4', '$foo5', '$foo6', '$foo7')");

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Parsing CSV files into a MySQL Table

2004-11-02 Thread Jason Wong
On Tuesday 02 November 2004 13:46, Mark Benson wrote:

> mysql_connect (localhost, foouser, foologin);

Always use check the return value of any mysql_*() function you use and make 
use of mysql_error().

> $csvfile = file("http://foo.com/foolist.csv";);

If you're using a newer version of PHP you can use fgetcsv() instead. Even 
better, if you're able to, use MySQL's LOAD DATA.

> foreach($csvfile as $line_no => $datastring) {

As you're not using $line_no the following is better:

  foreach ($csvfile as $datastring) {

>  mysql_query ("INSERT INTO 'footable' (foo1, foo2, foo3, foo4, foo5, foo6,
> foo7) VALUES '$foo1', '$foo2', '$foo3', '$foo4', '$foo5', '$foo6',
> '$foo7')"); }

Again, use mysql_error().

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-db
--
/*
 Michelle: You should be chief.
 Fry: What do I need, ulcers?
*/

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Parsing CSV files into a MySQL Table

2004-11-02 Thread Matt M.
> > $csvfile = file("http://foo.com/foolist.csv";);

and this 

fopen("http://foo.com/foolist.csv";, "r");

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Parsing CSV files into a MySQL Table

2004-11-02 Thread Matt M.
> // 
> 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')");
> }
> 
> //?>

try this

mysql_connect (localhost, foouser, foologin);

mysql_select_db (footest1);

$csvfile = file("http://foo.com/foolist.csv";);

while (($data = fgetcsv($csvfile, 1000, ",")) !== FALSE) {
   $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')");
}

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php