0

I want to read a csv file and put the data in a mysql database. The problem is that the "é" is badly encoded. I try to put this header :

header('Content-Type: text/html; charset=UTF-8');

But It didn't work.

Here is my code, the problème is on the variable $site :

<?php
include_once __DIR__ . '/../../../components/functions.php';
$filename = "../../../Factures/factures.csv";
header('Content-Type: text/html; charset=UTF-8');
if(($csvfile = fopen($filename, "r")) !== FALSE)
{
    fgets($csvfile);  fgets($csvfile);  fgets($csvfile);  fgets($csvfile);

    while (($data = fgetcsv($csvfile,0, ",")) !== FALSE)
    {
        $data = array_map("utf8_encode", $data);

        //Formattage des données
        $j = substr($data[3],0,2); $m = substr($data[3], 3,2); $y = substr($data[3], 6, 4);
        $dateDeb = $y . $m . $j;
        $j2 = substr($data[4],0,2); $m2 = substr($data[4], 3,2); $y2 = substr($data[4], 6, 4);
        $dateFin = $y2 . $m2 . $j2;
        $data[5] = str_replace(",", ".", $data[5]);
        $data[6] = str_replace(",", ".", $data[6]);
        $data[8] = str_replace(",", ".", $data[8]);
        print($data[0] . "\n");
        $req = $pdo->prepare('INSERT INTO Factures SET Site = :unSite, Type = :unType, Numero = :unNumero, DateFacture = :uneDateFacture, DateEcheance = :uneDateEcheance, MontantHT = :unMontantHT, TVA = :uneTVA, MontantTT = :unMontantTT, SoldeTTC = :unSoldeTTC');
        $req->execute(array(
            ':unSite' => utf8_encode($data[0]),
            ':unType' => $data[1],
            ':unNumero' => $data[2],
            ':uneDateFacture' => date("Y/m/d", strtotime($dateDeb)),
            ':uneDateEcheance' => date("Y/m/d", strtotime($dateFin)),
            ':unMontantHT' => round(doubleval($data[5]),2),
            'uneTVA' => round(doubleval($data[6]),2),
            'unMontantTT' => round(doubleval($data[5]+doubleval($data[6])),2),
            'unSoldeTTC' => round(doubleval($data[8]),2)
        ));
    }
    fclose($csvfile);
    //sup ou garder
}
Alex
  • 11
  • 1
  • _the problème is on the variable $site_ There is no variable `$site` in your code. _But It didn't work._ What didn't work? Do you get an error or corrupted database entries? Please be more specific. – Michel Jun 22 '21 at 09:30
  • And the `header('Content-Type: text/html; charset=UTF-8');` is useless as you're putting data into the database, and not outputting anything. Perhaps [this question](https://stackoverflow.com/q/279170/1685196) could help. – Michel Jun 22 '21 at 09:33
  • Sorry, it's on the variable $data[0]. I put my information in the database but here is the actual result : Clinique Vétérinaire. – Alex Jun 22 '21 at 09:43
  • If that's in your database, follow the given utf-8 guideluine step by step. Probably your database isn't utf-8, or the connection to the database isn't utf-8. Or the csv isn't utf-8. – Michel Jun 22 '21 at 09:49
  • when I connect to my db i set charset=utf8mb4. I tried to use mb_check_encoding() from php on my string and it returns 1=success which means it is right encoded – Alex Jun 22 '21 at 09:53
  • Did you also use `SET NAMES utf8`? – Michel Jun 22 '21 at 09:59
  • the problem was the array_map. Without it, it works well. Thank you ! – Alex Jun 22 '21 at 14:31

0 Answers0