首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用php和mysql将包含unicode数据的单个csv文件导入多个表

问无法使用php和mysql将包含unicode数据的单个csv文件导入多个表
EN

Stack Overflow用户
提问于 2019-08-27 23:57:42
回答 3查看 129关注 0票数 1

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

​

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

发布于 2019-08-28 00:36:53

您的文件是用制表符分隔的,而不是逗号分隔的,因此您需要告诉函数:

代码语言:javascript
复制
while(($filesop = fgetcsv($handle, 1000, "\t")) !== false)
票数 0
EN

Stack Overflow用户

发布于 2019-08-28 12:59:08

这是您的数据库Unicode格式的问题,请使用General_CI将您的数据库编码更改为UTF-8,并对目标表使用相同的编码。我的建议是使用Navicat来完成这项工作,因为转换MySQL数据库和表格式将非常简单。

记住要更改数据库的格式以及它的表。

票数 0
EN

Stack Overflow用户

发布于 2019-08-31 09:39:02

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

使用SELECT col, HEX(col) ...获取一些尼泊尔文本的十六进制,以便我们可以验证其编码。假设它使用的是天成文书字母表,我希望在十六进制中看到的主要是E0 A4 xx和E0 A5 xx。这是每个字符3个字节。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57678419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档