我正在尝试使用CSV文件更新mysql数据库。

我有此查询在phpmyadmin中工作

LOAD DATA INFILE 'C:/wamp/www/website/test_mysql_import.csv' INTO TABLE `csv_preset` FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n'


但是,当我尝试在mysqli_query函数中使用它时,它不会更新数据库。

这是完整的代码块:

<?php require "db_connect.php"; ?>
    <?php
    $csv_file = "C:/wamp/www/website/test_mysql_import.csv";

    $sql_dump ="TRUNCATE TABLE csv_preset";
    $sql_update = "LOAD DATA INFILE '$csv_file' INTO TABLE `csv_preset` FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n'");
    mysqli_query($db_conx, $sql_dump);
    mysqli_query($db_conx, $sql_update);
?>

最佳答案

<?php require "db_connect.php"; ?>
    <?php
    $csv_file = "C:/wamp/www/website/test_mysql_import.csv";

    $sql_dump ="TRUNCATE TABLE csv_preset";
    $sql_update = "LOAD DATA INFILE '".$csv_file."' INTO TABLE `csv_preset` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n'");
    mysqli_query($db_conx, $sql_dump);
    mysqli_query($db_conx, $sql_update);
?>

关于php - 如何在PHP的mysqli_query()中转义双引号字符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25865518/

10-11 05:46