我想读取一个 csv 文件并将数据放入 mysql 数据库中。问题是“é”编码错误。我试着把这个标题:
header('Content-Type: text/html; charset=UTF-8');
但它没有用。
这是我的代码,问题在变量 $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
}