如何在导入CSV文件到MySQL数据库中设置字符集?

时间:2019-07-17 14:04:29

标签: php mysql csv import cp1250

我写了一个代码,将数据从CSV文件导入数据库。但是,我在cp1250上设置字符集有问题。当我在数据库中使用波兰语字符时,会看到奇怪的字符。我试图使用SET NAMES函数,但是它不起作用-有什么想法吗?

<?php
$conn = mysqli_connect("localhost", "b", "c", "d");

if (isset($_POST["import"])) {

$fileName = $_FILES["file"]["tmp_name"];

if ($_FILES["file"]["size"] > 0) {

$file = fopen($fileName, "r");

while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {


$sqlInsert = "INSERT INTO evdb_Historia_Aktualizacja(ID,Imie_I_Nazwisko,Suma_Brutto_Przejazdy,Suma_Netto_Przejazdy,Gotowka_Przejazdy_Uber,Brutto_UberEats,Netto_UberEats,VAT_UberEats,Netto_Gotowka,Bonusy_Dodatki,Napiwki_Brutto,Napiwek_18,Napiwek,Dodatki_UberEats,Ilosc_Dni,Prowizja_Evelstar,Kwota_Po_Prowizji_Evelstar,VAT_Przejazdy,Faktury_Brutto,Faktury_Netto,VAT_Faktury,VAT_Nalezny,Suma_Bez_Skladek,Rodzaj_Umowy,Kwota_Skladek,Potracenia_Reczne,Potracenia_Stale,Przelew_Przewoz,Przelew_UberEats,Data_Od,Data_Do,Login)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "','" . $column[13] . "','" . $column[14] . "','" . $column[15] . "','" . $column[16] . "','" . $column[17] . "','" . $column[18] . "','" . $column[19] . "','" . $column[20] . "','" . $column[21] . "','" . $column[22] . "','" . $column[23] . "','" . $column[24] . "','" . $column[25] . "','" . $column[26] . "','" . $column[27] . "','" . $column[28] . "','" . $column[29] . "','" . $column[30] . "','" . $column[31] . "')";
$result = mysqli_query($conn, $sqlInsert);      
if (! empty($result)) {
$type = "success";
$message = "CSV Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
            }
        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

如果您的数据库是.dbf(或其他数据库),则可能是您的问题:

DBF - encoding cp1250