加载数据本地 INFILE MySQL/PHP 问题
这是我的 MySQL 表结构
id | tracking_number | order_id
这是 CSV 文件的结构: (有时 order_id 丢失,这似乎会导致问题)
"1R2689Y603406","33097"
"1R2689Y603404","33096"
"1R2689Y603414",
"1R2689Y603429","33093"
"1R2689Y603452",
这是我当前不起作用的 SQL 查询: (文件正在上传,并且正在被正确读取,是查询本身引起了问题)
$sql = 'LOAD DATA LOCAL INFILE "'.$_FILES['my_file']['tmp_name'].'"
INTO TABLE table_tracking
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY "\""
LINES TERMINATED BY "\n"
(tracking_number,order_id)';
mysql_query($sql) or die(myqsl_error());
我的查询有什么问题?感谢您的帮助!
编辑:更改了 CSV 结构以表示有时会发生的丢失数据。 更改查询以匹配我现在使用的查询
Here is my MySQL table structure
id | tracking_number | order_id
Here is the structure of the CSV file:
(Sometimes the order_id is missing, and this seems to be causing issues)
"1R2689Y603406","33097"
"1R2689Y603404","33096"
"1R2689Y603414",
"1R2689Y603429","33093"
"1R2689Y603452",
Here is my current SQL Query which isn't working:
(The file is being uploaded, and is being read correctly, it's the query itself which is causing issues)
$sql = 'LOAD DATA LOCAL INFILE "'.$_FILES['my_file']['tmp_name'].'"
INTO TABLE table_tracking
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY "\""
LINES TERMINATED BY "\n"
(tracking_number,order_id)';
mysql_query($sql) or die(myqsl_error());
What is wrong with my query? Thanks for any help!
Edit: Changed CSV structure to represent missing data that sometimes occurs.
Changed query to match the one I am now using
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是在这里尝试(不是 100% 自信),但是您是否尝试添加这样的目标列:
您的表中有三列,并且只提供两个数据。
Just trying here (not 100% confident) but did you try adding the destination columns like this:
You have three columns in your table and are only providing two data.
这对我有用:
您可能还需要确保第三列具有默认值。
This worked for me:
You also probably need to make sure that your third column has a default value.
而不是
$_FILES['my_file']['tmp_name']
,尝试dirname(__FILE__)
将给出完整的目录路径,如 'C:/path/to/directory '。Instead of
$_FILES['my_file']['tmp_name']
, trydirname(__FILE__)
will give the full directory path like 'C:/path/to/directory'.