Jump to content

fgetcsv - Import CSV into database - file CHARSET issue


AndieB

Recommended Posts

Hi all,

 

 

I'm going to load a CSV file and then take the data and do a MySQL insertion. Of course, that is just to import with MySQL, but I need to do some testing of the data first, in order to see into what table the data should be put. Positive values will go into one table and negative values into another.

 

Now, the CSV file I've got, is in UTF-8 charset. When testing this simple code:

<?php
$row = 1;
$handle = fopen("finance.csv", "r");
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>

 

I get a problem. It does not show the data correctly.

I've modified the script to look like this:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body>
<?php
$row = 1;
$handle = fopen("finance.csv", "r");
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>
</body>
</html>

 

But that did not help at all. What am I doing wrong?

How should I make sure that the CHARSET becomes correct?

 

Thank you for any kind of help!

 

--Andreas

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.