1

我正在使用该fgetcsv()功能导入 CSV,效果很好。

但是,当我查看数据库中的数据时,我看到带有问号的黑色菱形。当再次回显数据时,这不是什么大问题,因为它们不会出现,但是当我想使用其中一个 CSV 字段作为 MySQL 日期时,它不会被识别为日期并且是存储为0000-00-00.

例如

在此处输入图像描述

我认为这个问题与 CSV 的编码有关吗?任何人都可以提供任何建议吗?

谢谢!

编辑:如果有帮助,这里是我的导入脚本,根据 mb_detect_encoding,编码类型是 ASCII

<?php
include 'config.php';
include 'opendb.php';
ini_set("auto_detect_line_endings", true);

$row = 0;
$tmpName = $_FILES['csv']['tmp_name'];

if (($handle = fopen($tmpName, "r")) !== FALSE) {
    $num = count($data);

     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
        $noQuotes = str_replace("\"", '', $data);
        $originalDate = $noQuotes[1];

        //$delivery_date = date('Y-m-d', strtotime($originalDate));

        $parts = explode('/', $originalDate);
        $delivery_date = $parts[2] . '-' . $parts[1] . '-' . $parts[0];

        $row++;

        $import="INSERT into dispatch (delivery_note_number, delivery_date, dispatch_date, customer_delivery_date, delivery_line, produce, variety, quantity, pallets, count, depot, customer, grower, haulier, status) 
        values ('$noQuotes[0]', '$delivery_date', '$noQuotes[2]', '$noQuotes[3]', '$noQuotes[4]', '$noQuotes[5]', '$noQuotes[6]', '$noQuotes[7]', '$noQuotes[8]', '$noQuotes[9]', '$noQuotes[10]', '$noQuotes[11]', '$noQuotes[12]', '$noQuotes[13]', '$noQuotes[14]')";

        echo $import; 
        mysql_query($import) or die(mysql_error());
     }

        //header("location:list_dispatch.php?st=recordsadded");


    fclose($handle);
}

?>
4

3 回答 3

2

如果您的数据库使用的字符编码与 CSV 文件不同,则可能应首先转换数据。

执行此操作的一种方法是使用mb_convert_encoding()函数。

同样有用的是,mb_detect_encoding()应该能够检测给定输入字符串的字符编码。

于 2012-05-13T18:07:03.333 回答
0

有人在另一个论坛上找到了解决方案:

这些是 NUL 字符(ASCII 代码 0x00),像这样替换它们:

$string = str_replace(chr(0), '', $string);

于 2012-05-14T20:32:02.550 回答
0
fputcsv($f, $array, ' ', chr(0));
于 2013-05-22T14:44:30.750 回答