PHP/MySQL:导入 CSV,所有日期和十进制值都为空,零

Posted

技术标签:

【中文标题】PHP/MySQL:导入 CSV,所有日期和十进制值都为空,零【英文标题】:PHP/MySQL: importing CSV, all date and decimal value are null, zero 【发布时间】:2014-04-15 19:34:31 【问题描述】:

我在通过 php 将 CSV 数据导入 mysql 时遇到问题。所有导入的日期和十进制值(不是 varchar 或 INT)在 MySQL 中显示为 0 或 0000-00-00(null?)。当我回显它们时,CSV 的 PHP 处理返回正确的值,我没有得到 mysqli_error(),所以我认为这是一个 MySQL 问题。

这是我的数据表配置

如果需要我可以提供代码

编辑

CSV 用“|”表示

while(($fileop = fgetcsv($handle,1000,"|")) !== false) //code

CSV 文件

account_name|account_cycle|bal_date|bal_month_sales|bal_month_returns|bal_month_net|bal_YTD_sales|bal_YTD_returns|bal_YTD_net|bal_lastyear_sales|bal_lastyear_returns|bal_lastyear_net|||
Jeweler 1|5/22/2013|3/31/2014|$306.00|$0.00|$306.00|$306.00|$0.00|$306.00|$404.00|$0.00|$404.00|||
Jeweler 2|5/12/2012|3/31/2014|$70.00|$0.00|$70.00|$178.00|$0.00|$178.00|$0.00|$0.00|$0.00|||
Jeweler 3|1/1/2012|3/31/2014|$52.00|$0.00|$52.00|$9,109.50|$0.00|$9,109.50|$895.20|$0.00|$895.20|||
Jeweler 4|3/16/2012|3/31/2014|$18.60|$0.00|$18.60|$575.80|$0.00|$575.80|$1,982.80|-$68.02|$1,914.78|||
Jeweler 5|2/5/2011|3/31/2014|$292.00|$0.00|$292.00|$2,725.95|-$596.98|$2,128.97|$323.00|$0.00|$323.00|||
Jeweler 6|7/4/2012|3/31/2014|$0.00|$0.00|$0.00|$2,463.00|-$1,108.69|$1,354.31|$2,324.90|-$996.50|$1,328.40|||
Jeweler 7|9/26/2013|3/31/2014|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$2,553.00|-$1,206.02|$1,346.98|||
Jeweler 8|3/28/2011|3/31/2014|$168.00|$0.00|$168.00|$334.00|$0.00|$334.00|$564.00|$0.00|$564.00|||
Jeweler 9|3/1/2011|3/31/2014|$0.00|$0.00|$0.00|$1,400.00|$0.00|$1,400.00|$3,332.00|-$637.82|$2,694.18|||
Jeweler 10|12/15/2011|3/31/2014|$428.00|$0.00|$428.00|$1,198.75|$0.00|$1,198.75|$36.00|$0.00|$36.00|||
Jeweler 11|12/1/2012|3/31/2014|$312.00|$0.00|$312.00|$1,504.00|$0.00|$1,504.00|$0.00|$0.00|$0.00|||
Jeweler 12|6/5/2013|3/31/2014|$0.00|$0.00|$0.00|$0.00|-$981.54|-$981.54|$1,818.11|$0.00|$1,818.11|||
Jeweler 13|5/23/2013|3/31/2014|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|||
Jeweler 14|8/31/2012|3/31/2014|$0.00|$0.00|$0.00|$142.00|$0.00|$142.00|$396.95|$0.00|$396.95|||
Jeweler 15|4/14/2013|3/31/2014|$2,657.80|$0.00|$2,657.80|$2,745.80|$0.00|$2,745.80|$672.80|$0.00|$672.80|||
Jeweler 16|8/22/2012|3/31/2014|$2,292.80|$0.00|$2,292.80|$2,708.80|$0.00|$2,708.80|$8,219.75|$0.00|$8,219.75|||
Jeweler 17|11/11/2010|3/31/2014|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|||
Jeweler 18|1/31/2012|3/31/2014|$0.00|$0.00|$0.00|$2,330.00|$0.00|$2,330.00|$3,093.42|$0.00|$3,093.42|||
Jeweler 19|2/1/2011|3/31/2014|$0.00|$0.00|$0.00|$60.00|$0.00|$60.00|$144.00|$0.00|$144.00|||
Jeweler 20|7/16/2013|3/31/2014|$58.00|-$30.40|$27.60|$2,757.40|-$979.26|$1,778.14|$1,953.60|$0.00|$1,953.60|||
Jeweler 21|9/28/2012|3/31/2014|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|||
Jeweler 22|10/11/2012|3/31/2014|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$108.00|$0.00|$108.00|||
Jeweler 23|5/3/2010|3/31/2014|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|||
Jeweler 24|7/23/2012|3/31/2014|$118.00|$0.00|$118.00|$246.00|$0.00|$246.00|$0.00|$0.00|$0.00|||

【问题讨论】:

也许 csv 将这些字段存储为字符串。你能从csv中给出一个示例行吗 您是如何尝试导入 CSV 的? oohhh dam 忘记取出美元了,已经提上日程了 美元符号。 不仅如此。内容类型与其预期格式不匹配。您确定日期数据类型可以接受 m/d/Y 导入格式吗?我认为它只会接受 Y-m-d,但我可能是错的 【参考方案1】:

需要从csv中取出“$”

但是,日期值仍然是 0000-00-00,估计我得做一些格式更改

编辑 使用date('Y-m-d', strtotime(str_replace('-', '/', $originalDate))); 修复日期格式

【讨论】:

【参考方案2】:

您的日期格式错误,请改用 Y-m-d (2001-11-00)

【讨论】:

以上是关于PHP/MySQL:导入 CSV,所有日期和十进制值都为空,零的主要内容,如果未能解决你的问题,请参考以下文章

如何以 dd/mm/yyyy hh:mm 格式从 CSV 导入日期到 R 中的通用数字字符串

CSV 日期格式转 MySQL 日期格式

Pandas CSV 导入中二进制变量的最佳数据类型,以减少内存使用

csv导入到数据库(目标参考注释)

如何从导入的 csv 文件中索引日期时间列 - pandas

phpmyadmin 导入日期格式困难的 csv