CSV problems

This is a discussion on CSV problems within the PHP Language forums, part of the PHP Programming Forums category; Here's one for you. I import a CSV file which is then read using fgetcsv(); My problem is that ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-17-2003
Richard Grove
 
Posts: n/a
Default CSV problems

Here's one for you.
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the
whole thing bugs out.
I am wondering of it is possible to replace these commas in the fields
without replacing the CSV structure.

Any help greatly appreciated.

Regards
Richard Grove


http://shopbuilder.org - ecommerce systems
Become a Shop Builder re-seller:
http://www.affiliatewindow.com/affil...ls.php?mid=611
http://www.affiliatewindow.com/a.pl?590





---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 03/11/2003


Reply With Quote
  #2 (permalink)  
Old 11-17-2003
Rob Allen
 
Posts: n/a
Default Re: CSV problems

In message <3fb8e3c9$0$108$65c69314@mercury.nildram.net>, Richard Grove
<info@[at].invalid> writes
>Here's one for you.
>I import a CSV file which is then read using fgetcsv();
>My problem is that if the fields in the CSV file have commas in them, the
>whole thing bugs out.
>I am wondering of it is possible to replace these commas in the fields
>without replacing the CSV structure.
>


In general a CSV format file should have double-quote marks around the
field data if it has commas in it.

E.g.

1,2,3,some test data,5
1,2,3,"some, test data",5

fgetcsv() should then read this correctly.

--
Rob Allen
Reply With Quote
  #3 (permalink)  
Old 11-17-2003
Michael Fuhr
 
Posts: n/a
Default Re: CSV problems

"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:

> I import a CSV file which is then read using fgetcsv();
> My problem is that if the fields in the CSV file have commas in them, the
> whole thing bugs out.


What exactly happens when "the whole thing bugs out"? Are the
commas in the fields inside quotation marks? Are there quotation
marks in the file that could be confusing fgetcsv()?

Could you post a line from the CSV file and the code you're using
to read it? That way we could see exactly what's happening.

> I am wondering of it is possible to replace these commas in the fields
> without replacing the CSV structure.


Let's get all the facts before looking for a solution.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Reply With Quote
  #4 (permalink)  
Old 11-17-2003
Richard Grove
 
Posts: n/a
Default Re: CSV problems

"Michael Fuhr" <mfuhr@fuhr.org> wrote in message
news:3fb8f879$1_1@omega.dimensional.com...
> "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
>
> > I import a CSV file which is then read using fgetcsv();
> > My problem is that if the fields in the CSV file have commas in them,

the
> > whole thing bugs out.

>
> What exactly happens when "the whole thing bugs out"? Are the
> commas in the fields inside quotation marks? Are there quotation
> marks in the file that could be confusing fgetcsv()?
>
> Could you post a line from the CSV file and the code you're using
> to read it? That way we could see exactly what's happening.
>
> > I am wondering of it is possible to replace these commas in the fields
> > without replacing the CSV structure.

>
> Let's get all the facts before looking for a solution.
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/




My CSV file doesn't have quotes.
When it bugs out, what I meant to say was that when it populates the
databse, it skips columns and puts values in the wrong place.
Not really bugging out I suppose.

Regards
Richard Grove
01892 546979


http://shopbuilder.org - ecommerce systems
Become a Shop Builder re-seller:
http://www.affiliatewindow.com/affil...ls.php?mid=611
http://www.affiliatewindow.com/a.pl?590





---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 03/11/2003


Reply With Quote
  #5 (permalink)  
Old 11-17-2003
Michael Fuhr
 
Posts: n/a
Default Re: CSV problems

"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:

> "Michael Fuhr" <mfuhr@fuhr.org> wrote in message
> news:3fb8f879$1_1@omega.dimensional.com...
> > "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
> >
> > > I import a CSV file which is then read using fgetcsv();
> > > My problem is that if the fields in the CSV file have commas in them,

> the
> > > whole thing bugs out.

> >
> > What exactly happens when "the whole thing bugs out"? Are the
> > commas in the fields inside quotation marks? Are there quotation
> > marks in the file that could be confusing fgetcsv()?
> >
> > Could you post a line from the CSV file and the code you're using
> > to read it? That way we could see exactly what's happening.

>
> My CSV file doesn't have quotes.


If a comma is the field separater, and if a field's value contains
commas, then that field needs to be quoted. Without some way to
distinguish which commas are part of a field and which commas
separate different fields, you'll get unexpected results.

> When it bugs out, what I meant to say was that when it populates the
> databse, it skips columns and puts values in the wrong place.
> Not really bugging out I suppose.


Make sure you're reading the data correctly before worrying about
how it's inserted into a database.

As I asked before, please post a troublesome line from the CSV file
and the code you're using to read it. If we can see what you're
doing then we might be able to offer advice on how to fix the
problem.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Reply With Quote
  #6 (permalink)  
Old 11-17-2003
Matthew Crouch
 
Posts: n/a
Default Re: CSV problems

"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> wrote in message
news:3fb8fe02$0$119$65c69314@mercury.nildram.net.. .
> "Michael Fuhr" <mfuhr@fuhr.org> wrote in message
> news:3fb8f879$1_1@omega.dimensional.com...
> > "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
> >
> > > I import a CSV file which is then read using fgetcsv();
> > > My problem is that if the fields in the CSV file have commas in them,

> the
> > > whole thing bugs out.

> >
> > What exactly happens when "the whole thing bugs out"? Are the
> > commas in the fields inside quotation marks? Are there quotation
> > marks in the file that could be confusing fgetcsv()?
> >
> > Could you post a line from the CSV file and the code you're using
> > to read it? That way we could see exactly what's happening.
> >
> > > I am wondering of it is possible to replace these commas in the fields
> > > without replacing the CSV structure.

> >
> > Let's get all the facts before looking for a solution.
> >
> > --
> > Michael Fuhr
> > http://www.fuhr.org/~mfuhr/

>
>
>
> My CSV file doesn't have quotes.
> When it bugs out, what I meant to say was that when it populates the
> databse, it skips columns and puts values in the wrong place.
> Not really bugging out I suppose.
>


Sounds like the csv is not being properly generated. Without those quotes,
there's really no way to know where you're trying to separate the fields.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:00 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0