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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.