having some problems reading a 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
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

having some problems reading a csv file

Post by scheinarts »

Hi guys,

Im trying to read in a csv file, parse it and put all the data in the mysql db. I've tried reading/opening the csv file many ways (fopen, fgetcsv, file) and I always the same result.
Every time, every single word, number or phrase gets divided by each character and a diamond with a question mark is inserted in between character. That is how it is saved in the db, and thats how its displayed on the browser. But when you read the actual csv file, everything looks normal.

This simple debug test below shows on the browser just fine the entire line.

Code: Select all

function opencsv($file)
{
    $lines = file($file);
    
    foreach ($lines as $pos => $line)
    {
        echo $line;
    }
}
A simple explode(",", $line) turns every item extracted like I described above.

Im not sure exactly what the issue is or how to fix. Has this happened to some one?

Many thanks!
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Re: having some problems reading a csv file

Post by scheinarts »

Here is a little screen shot of what it looks like
Attachments
Image1.jpg
Image1.jpg (11.54 KiB) Viewed 234 times
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: having some problems reading a csv file

Post by VladSun »

If you have "normal" access to your MySQL server, try:
[sql]LOAD DATA LOCAL INFILE '/importfile.csv' INTO TABLE my_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1, filed2, field3);[/sql]

and be sure your MySQL server instance is running with --local-infile=1
There are 10 types of people in this world, those who understand binary and those who don't
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Re: having some problems reading a csv file

Post by scheinarts »

Hi VladSun thanks for the response. I cannot use that approach because there is some other information generated in the script that needs to go in the database along with the csv information. Any ideas on that?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: having some problems reading a csv file

Post by VladSun »

Insert this "php-side" information after you have loaded into your DB your CSV file ...
I would suggest that nothing you had generated in you PHP script, cant' be performed in a SQL query in your case.

Otherwise, explain in details, please.
There are 10 types of people in this world, those who understand binary and those who don't
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Re: having some problems reading a csv file

Post by scheinarts »

I should have mentioned that I dont have that kind of access with my db.
What can I do in this case? Thanks for any ideas you can give. I've been breaking my head on this since yesterday and still cant pass foward it.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: having some problems reading a csv file

Post by VladSun »

OK...
Try exporting, then importing in UTF-8 charset.
I'm pretty sure it's due to charset encoding.
There are 10 types of people in this world, those who understand binary and those who don't
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Re: having some problems reading a csv file

Post by scheinarts »

damn my file is 8.06 mb and phpmyadmin upload limit it 8mb so it gave me the file is too large error. I tried uploading it zipped (the csv file that is) but it said it did not recognize the columns.
Post Reply