SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CSV import when there are commas in the text?

    Hi

    I'm trying to grab a csv file from a remote server and import the data my mySQL DB.

    I'm using Curl to grab the file contents and php to import the data into my DB.

    The problem have is that the data in the csv file contains commas in the text field that is interferring with the import and I have no control over what is in the file.

    eg the csv looks like:

    Code:
    id,name,description1,description2,date
    1,pete,"this has a comma, in the text", "this also has, a comma", 2004-09-09
    etc, etc
    The additional commas mess up the import. Is there any way I can get this data into mysql and just tell it to ignore any commas in certain fields?

    Thanks

  2. #2
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll bite, don't use php to import the file. Use LOAD DATA INFILE and import the file directly into mysql.

  3. #3
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    I did try using the "CSV using LOAD DATA" option in phpMyadmin but the additional commas still broke the import if I used Fields enclosed by " or not.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Strange. Commas in quoted fields are CSV spec... if something is able to read CSV, it shouldn't have any problem with that.

    PHP's fgetcsv() doesn't.

  5. #5
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I've not used fgetcsv before. I'll look into it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •