Problem importing CSV file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stevietee
Forum Newbie
Posts: 16
Joined: Fri Apr 14, 2006 9:24 am

Problem importing CSV file

Post by stevietee »

Straightforward noob question I hope. Can anyone confirm for me if php has a problem parsing csv files containing '&'. Im trying to import from a file that contains urls, subsequently loading them into a database. Im getting a syntax error in the SQL command that performs the load, which when echoed to the screen looks like:

insert into TABLE values ('0','blahblah' ,'http://www.domain.com/page?a=1221696&b=17328=12227345','4.59','0','some text.','some more text','999')

The csv file contains the data as follows
'0'|'blahblah' |'http://www.domain.com/page?a=1221696&b=17328&prod=12227345'|'4.59'|'0'|'some text.'|'some more text'|'999'

The section of code hilighted in red is my concern the &prod seems to be getting converted to ∏? I am using fgets() to import and explode to assign the values to an array. Is this the correct approach or is there another method I should use. (Note the CSV file is actually double quoted not single quoted as shown)

TIA.
Stephen.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You need to use the escape function for the database you are using on the values in the insert. Check the PHP manual.
(#10850)
Post Reply