PHP - 使用LOAD INTO INFILE将csv导入mysql后行的格式不正确

时间:2017-10-25 12:02:03

标签: php mysql

我是新来的。我想问一下有关我的代码的问题。所以我的代码工作得很好但是之后使用LOAD INTO INFILE以编程方式将csv导入mysql。我不知道为什么我的输出会保持这种格式。请看下面的代码谢谢!

$testing = $conn->prepare("LOAD DATA INFILE 'C:/xampp/mysql/data/mysql/usagetable.csv'
            INTO TABLE trieinitialcountentry
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY '\n'
            IGNORE 1 LINES
            (Count_ID, InvItemLocID, OnHand, OnHandValue, StockCenter_ID)");
$testing->execute();

而且,有些行已经完美导入,但其中一些不是。我的输出示例:

Count_ID InvItemLocID OnHand OnHandValue StockCenter_ID
737450   -2091889269  140.00  "2         788.80"

1 个答案:

答案 0 :(得分:0)

显然,输入CSV文件中的某些值包含在引号中,但您未将此信息传递给LOAD DATA INFILE

尝试此查询:

$query = <<<'END'
    LOAD DATA INFILE 'C:/xampp/mysql/data/mysql/usagetable.csv'
    INTO TABLE trieinitialcountentry
    FIELDS TERMINATED BY ','
           OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (Count_ID, InvItemLocID, OnHand, OnHandValue, StockCenter_ID)
END;

$testing = $conn->prepare($query);
$testing->execute();

请注意添加的OPTIONALLY ENCLOSED BY '"'子句。