加载数据本地 INFILE MySQL/PHP 问题

发布于 2024-12-08 19:53:10 字数 722 浏览 0 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随梦而飞# 2024-12-15 19:53:10

只是在这里尝试(不是 100% 自信),但是您是否尝试添加这样的目标列:

mysql_query("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)");

您的表中有三列,并且只提供两个数据。

Just trying here (not 100% confident) but did you try adding the destination columns like this:

mysql_query("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)");

You have three columns in your table and are only providing two data.

緦唸λ蓇 2024-12-15 19:53:10

这对我有用:

$sql = 'load data local infile "c:/users/ramon/desktop/1.csv"
        into table test fields terminated by ","
        optionally enclosed by "\""
        lines terminated by "\n"';

mysql_query($sql) or die(myqsl_error());

您可能还需要确保第三列具有默认值。

This worked for me:

$sql = 'load data local infile "c:/users/ramon/desktop/1.csv"
        into table test fields terminated by ","
        optionally enclosed by "\""
        lines terminated by "\n"';

mysql_query($sql) or die(myqsl_error());

You also probably need to make sure that your third column has a default value.

扭转时空 2024-12-15 19:53:10

而不是 $_FILES['my_file']['tmp_name'],尝试

dirname(__FILE__)."/".$_FILES['my_file']['tmp_name']

dirname(__FILE__) 将给出完整的目录路径,如 'C:/path/to/directory '。

Instead of $_FILES['my_file']['tmp_name'], try

dirname(__FILE__)."/".$_FILES['my_file']['tmp_name']

dirname(__FILE__) will give the full directory path like 'C:/path/to/directory'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文