【问题标题】:Unable to import single csv file that contains unicode data to multiple tables using php and mysql无法使用 php 和 mysql 将包含 unicode 数据的单个 csv 文件导入多个表
【发布时间】:2019-12-31 20:32:25
【问题描述】:

我正在尝试使用 php 和 mysql 将包含 unicode 数据的单个 csv 文件导入到多个表中。我能够将数据插入到多个表中,但不支持 unicode。我有带有尼泊尔语数据的 csv 文件。请帮我。 提前致谢

<?php
header('Content-Type: charset=utf-8');
  require_once('conn.php');
if(isset($_POST["submit"])){

          $file = $_FILES['file']['tmp_name'];
          $handle = fopen($file, "r");
          // echo $file;die;
          $c = 0;
          while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
          // while(($filesop = fgetcsv($handle, 1000, "\t")) !== false)
                    {
                         $filesop = array_map("utf8_encode", $filesop);
          // var_dump($filesop);die;
          $id = $filesop[0];
          // echo $id;die;
          $registration_date = $filesop[1];

          $building_use_id = $filesop[2];
          $building_category = $filesop[3];


          $a=utf8_encode($registration_date);

          $bps_registration_newregistration = "insert into bps_registration_newregistration(id,registration_date) values ('$id','$registration_date')";
          $stmt = mysqli_prepare($conn,$bps_registration_newregistration);
          mysqli_stmt_execute($stmt);
 $bps_registration_application = "insert into bps_registration_application(reg_id,registration_date,building_use_id,building_category) values ('$id','$a','$building_use_id','$building_category')";
          $stmt1 = mysqli_prepare($conn,$bps_registration_application);
          mysqli_stmt_execute($stmt1);


         $c = $c + 1;
           }

            if($bps_registration_newregistration  && $bps_registration_application ){
               echo "sucess";
             } 

     else
     {
            echo "Sorry! Unable to impo.";
          }
}
?>
<!DOCTYPE html>
<html>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<body>
<form enctype="multipart/form-data" method="post" role="form" accept-charset="UTF-8">
    <div class="form-group">
        <label for="exampleInputFile">File Upload</label>
        <input type="file" name="file" id="file" size="150">
        <p class="help-block">Only Excel/CSV File Import.</p>
    </div>
    <button type="submit" class="btn btn-default" name="submit" value="submit">Upload</button>
</form>
</body>
</html>


【问题讨论】:

  • 这对您有帮助吗? mathiasbynens.be/notes/mysql-utf8mb4
  • 你能在文本文件中显示 CSV 的第一行吗?也许它是制表符而不是逗号分隔,或者使用另一个分隔符
  • 我将 csv 文件转换为文本文件,并添加了有问题的图像。 @aynber
  • 我在 mysql 数据库中使用了 utf-8,但它仍然给我带来了问题@Marc Dix
  • 是的,它是制表符分隔而不是逗号分隔。

标签: php mysql csv unicode import-from-excel


【解决方案1】:

你的文件是制表符分隔的,不是逗号分隔的,所以你需要告诉函数:

while(($filesop = fgetcsv($handle, 1000, "\t")) !== false)

【讨论】:

  • 我仍然遇到同样的问题。如何解决它以及unicode问题? @aynber
  • 它仍然给你未定义的偏移量?您可能需要var_dump($filesop) 才能查看其中包含的所有内容。
  • 当我在 while 循环中进行 var_dump 并上传 csv 文件并单击上传按钮时,它说我将文件保存在我的计算机中。 @aynber
  • 这很奇怪。它不应该要求你下载任何东西
  • 但我无法从 csv 文件中获取数据。它只显示 header 。 @aynber array(1) { [0]=> string(84) "id,registration_date,contactno,latitude,building_use_id,building_category,ch_name_np" }
【解决方案2】:

这是您的数据库 Unicode 格式问题,请将您的数据库编码更改为带有 General_CI 的 UTF-8,并且对于目标表也是如此。我的建议是使用 Navicat 来执行此操作,因为转换 MySQL 数据库和表格式很简单。

记得更改数据库及其表格的格式。

【讨论】:

    【解决方案3】:

    不要使用 fgetcsv 编写 PHP 代码,而是使用 MySQL 的 LOAD DATA INFILE 将其直接拉入表中。请务必使用CHARACTER SET utf8(或utf8mb4)创建表。

    使用SELECT col, HEX(col) ... 获取一些尼泊尔语文本的十六进制,以便我们验证其编码。假设它使用的是梵文字母,我希望看到大部分是十六进制的E0 A4 xxE0 A5 xx。每个字符 3 个字节。

    即使您通过 PHP 代码加载它,也要以这种方式验证表格。如果您有问题,请参阅Trouble with UTF-8 characters; what I see is not what I stored

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 1970-01-01
      • 2018-01-05
      • 2012-05-15
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      相关资源
      最近更新 更多