Page 1 of 1

having some problems reading a csv file

Posted: Sun Sep 14, 2008 3:55 pm
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!

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 3:59 pm
by scheinarts
Here is a little screen shot of what it looks like

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 5:57 pm
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

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 6:23 pm
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?

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 6:39 pm
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.

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 6:50 pm
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.

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 7:00 pm
by VladSun
OK...
Try exporting, then importing in UTF-8 charset.
I'm pretty sure it's due to charset encoding.

Re: having some problems reading a csv file

Posted: Sun Sep 14, 2008 7:11 pm
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.