使用Symfony 3,XAMPP和php 5.6
我有一个表单,用户可以上传.csv文件,并且应该将行插入表格
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('memberList')->getData();
$db = $this->getDoctrine()->getEntityManager()->getConnection();
$sql = "LOAD DATA INFILE '$file' INTO TABLE `MemberList` CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;";
$stmt = $db->prepare($sql);
$stmt->execute();
}
然后我收到以下错误
执行'LOAD DATA INFILE'时出现异常C:\ xampp \ tmp \ php198.tmp'INTO TABLE
MemberList
字符集'utf8'字段终止于',''''''''''by by by by by by by by by by by by by by by by by by by by by by by “ 'IGNORE 1 LINES;':SQLSTATE [HY000]:常规错误:29找不到文件'C:\ xampp \ mysql \ data \ xampp \ tmpphp198.tmp'(错误代码:22“无效参数”)
我在调试器中逐步完成了代码,文件已成功上传到C:\xampp\tmp\php198.tmp
。我不知道为什么它在C:\xampp\mysql\data\xampp mpphp198.tmp
寻找一个文件,这似乎导致了这个问题,因为那里没有任何文件
更新:尝试使用LOAD DATA LOCAL INFILE
,现在获得
SQLSTATE [HY000]:常规错误:7890无法找到文件'C:xampp mpphp2F24.tmp'。
正在创建的临时文件实际上是C:\ xampp \ tmp \ php2F24.tmp
更新2:我是个白痴,它显然是在逃避反斜杠 - 但为什么呢?我认为准备PDO会留下这些完整的
答案 0 :(得分:-1)
管理让它使用此
$db = $this->getDoctrine()->getEntityManager()->getConnection();
$sql = "LOAD DATA INFILE ? INTO TABLE member_list CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;";
$pdo = $db->prepare($sql);
$pdo->execute(array($file));
$pdo->closeCursor();