我一直在编写一个脚本,将CSV文件中的数据导入phpMyAdmin数据库表。
使用此代码正常工作:
$query = <<<eof
LOAD DATA LOCAL INFILE '$file'
INTO TABLE c170720
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
eof;
但是,如果我使用它,它不起作用:
$query = <<<eof
LOAD DATA LOCAL INFILE '$file'
INTO TABLE '$m_cfile'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
eof;
这是整个代码:
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "smposf";
$con = mysqli_connect($host,$user,$password,$db);
$message = "";
if (isset($_POST['submit3']))
{
$m_date=date($sdate);
$m_yy=substr($m_date,2,2);
$m_mm=substr($m_date,5,2);
$m_dd=substr($m_date,8,2);
$m_cfile='c'.$m_yy.$m_mm.$m_dd;
$allowed = array('csv');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed))
{
// show error message
$message = 'Invalid file type, please use .CSV file!';
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "CSV/" .
$_FILES['file']['name']);
$file = "CSV/" . $_FILES['file']['name'];
$query = <<<eof
LOAD DATA LOCAL INFILE '$file'
INTO TABLE '$m_cfile'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
eof;
if (!$result = mysqli_query($con, $query))
{
exit(mysqli_error($con));
}
$message = "CSV file successfully imported!";
}
}
?>
如果我使用
LOAD DATA LOCAL INFILE '$file'
INTO TABLE c170720
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ccode,custarea,ttlpkt)
它完全正常,但我需要变量$m_cfile
。这是因为用户将每天使用特定日期创建文件。怎么了?
答案 0 :(得分:0)
将INTO TABLE '$m_cfile'
更改为INTO TABLE $m_cfile