如何使用php中的加载数据导入csv文件?

时间:2019-06-11 08:40:03

标签: php

我的csv文件包含数百万条记录,但是当我执行脚本时,它给我错误

  

致命错误:未捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第8行的“ ID,姓氏,名字,全名,电话,扩展名,电子邮件,部门,站点,房间,职位名称,图像”附近使用/opt/lampp/htdocs/reader/index.php:31堆栈跟踪:#0 /opt/lampp/htdocs/reader/index.php(31):PDO-> exec('\ nLOAD DATA LOCA ...') #1 {main}在第31行的/opt/lampp/htdocs/reader/index.php中抛出

这是我的代码

<?php

include_once 'connection.php';

$handle = fopen('demo.csv', 'r');
$header = fgetcsv($handle,1000,",");

if($header){
    $header_sql = array();
    foreach($header as $h) {
        $header_sql[] = $h;
    }


$sql = "
LOAD DATA LOCAL INFILE 'demo.csv'
REPLACE INTO TABLE `imported_csv`
FIELDS
TERMINATED BY '\t'
LINES
TERMINATED BY '\r\n'
IGNORE 1 LINES
(".implode(',', $header_sql).")";


   echo $sql;

   $affectedRows = $pdo->exec($sql);

   echo "Loaded a total of $affectedRows records from this csv file.\n";

  ?>

0 个答案:

没有答案