将包含其他逗号的CSV导入MySQL

时间:2018-10-30 11:30:05

标签: mysql fgetcsv

我的CSV文件在特定列中还有一些其他逗号,这引起了问题。当我将其导入MySQL时,该特定列中的数据分为2列。您能帮我解决这个问题吗?谢谢。

注意1:不允许在服务器上使用“加载数据本地文件”,因此不建议使用此选项。

注意2:我不太精通PHP,因此如果您显示正确的代码,我将不胜感激。

注意3.其他逗号在$ Club_Name列中。但是,将来可能还会在其他列中出现其他逗号,因此,我正在寻找一种完整的解决方案,以清除所有列中所有不相关的逗号。

我的代码(经过几天的努力):

<?php 
include("/path/to/the/database/connection/db_connect.php");

$filename = fopen("/path/to/the/csv/file/clubperformance_District_102.csv", "r");
//open the csv file
if ($filename) {
$firstline = fgets($filename, 4096);
    while (($line = fgets($filename)) !== false) {
         $lineArr = explode(",", "$line");
         var_dump($lineArr); // to display the array
         $lineArr = preg_replace("/[^a-zA-Z0-9 ]/", "", $lineArr); //clean up everything except alpha numeric characters & spaces

// list all the variables representing the database columns            
list($District, $Division, $Area, $Club_Number, $Club_Name, $Club_Status, $Mem_Base, $Active_Members, $Goals_Met, $CCs, $Add_CCs, $ACs, $Add_ACs, $CL_AL_DTMs, $Add_CL_AL_DTMs, $Level_1, $Level_2, $Add_Level_2, $Level_3, $Level_4, $Level_5, $New_Members, $Add_New_Members, $Off_Trained_Round_1, $Off_Trained_Round_2, $Mem_dues_on_time_Oct, $Mem_dues_on_time_Apr, $Off_List_On_Time, $Club_Distinguished_Status, $Last_Updated) = $lineArr;

// and then insert data
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `toastmaster_dcp_tracking_old`(`District`, `Division`, `Area`, `Club_Number`, `Club_Name`, `Club_Status`, `Mem_Base`, `Active_Members`, `Goals_Met`, `CCs`, `Add_CCs`, `ACs`, `Add_ACs`, `CL_AL_DTMs`, `Add_CL_AL_DTMs`, `Level_1`, `Level_2`, `Add_Level 2`, `Level_3`, `Level_4`, `Level_5`, `New_Members`, `Add_New_Members`, `Off_Trained_Round_1`, `Off_Trained_Round_2`, `Mem_dues_on_time_Oct`, `Mem_dues_on_time_Apr`, `Off_List_On_Time`, `Club_Distinguished_Status`, `Last_Updated`) VALUES ('$District', '$Division', '$Area', '$Club_Number', '$Club_Name', '$Club_Status', '$Mem_Base', '$Active_Members', '$Goals_Met', '$CCs', '$Add_CCs', '$ACs', '$Add_ACs', '$CL_AL_DTMs', '$Add_CL_AL_DTMs', '$Level_1', '$Level_2', '$Add_Level_2', '$Level_3', '$Level_4', '$Level_5', '$New_Members', '$Add_New_Members', '$Off_Trained_Round_1', '$Off_Trained_Round_2', '$Mem_dues_on_time_Oct', '$Mem_dues_on_time_Apr', '$Off_List_On_Time', '$Club_Distinguished_Status', '$Last_Updated')")or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));

    }

    fclose($filename);
}
?>

1 个答案:

答案 0 :(得分:0)

基本上,这些是我编辑的行。其余的都一样。希望它可以帮助某人。

if ($filename) {
    $firstline = fgetcsv($filename, 1000);
        while (($line = fgetcsv($filename)) !== false) {
        var_dump($line); // to display the array
             $line = preg_replace("/[^a-zA-Z0-9 ]/", "", $line); //clean up the mess, if any

    // list all the variables representing the database columns          
    list($District, $Division, $Area, $Club_Number, $Club_Name, $Club_Status, $Mem_Base, $Active_Members, $Goals_Met, $CCs, $Add_CCs, $ACs, $Add_ACs, $CL_AL_DTMs, $Add_CL_AL_DTMs, $Level_1, $Level_2, $Add_Level_2, $Level_3, $Level_4, $Level_5, $New_Members, $Add_New_Members, $Off_Trained_Round_1, $Off_Trained_Round_2, $Mem_dues_on_time_Oct, $Mem_dues_on_time_Apr, $Off_List_On_Time, $Club_Distinguished_Status, $Last_Updated) = $line;