无法将CSV文件放入数据库PHP

时间:2016-08-14 10:49:43

标签: php mysql database csv

我编写了以下PHP代码以将CSV文件加载到数据库中。但是查询没有执行。

<?php
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

if(mysqli_select_db($conn,"test"))
{
    printf("Success");
}

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "LOAD DATA INFILE 'output.csv' INTO TABLE new COLUMNS TERMINATED BY ','  ENCLOSED BY '\"\' ESCAPED BY '\"\' " ;
printf($query);

if(mysqli_query($conn,$query))
{
printf("query executed");
}

mysqli_close($conn);
?>

请告诉我是什么问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我感觉这来自于从常规文本到PHP字符串的错误转换:

ENCLOSED BY '\"\' ESCAPED BY '\"\'

这应该是:

ENCLOSED BY '\"' ESCAPED BY '\"'

查询本身无效。您应该收到错误。