2017-04-05 3 views
1

enter image description herePHP에서 CSV에서 가져 오기 (mysql)는 '???'

CSV에서 가져올 때 나에게 ??? 내가 CSV에서 가져 오기 및 코드 아래 사용할 때 내가 *

을 선택 않지만 때

내 DB와 테이블이 aleady UTF8 유니 코드 CI 및 아랍어 및 기타 데이터로 설정되어 제대로 보여줍니다. 그것은 나를 성공적으로 업로드했지만 데이터가 ???로 표시됩니다. 데프 아랍어 하나 محمود عبدالعزيز

1 ABC를 다음과 같이 직원 이름에

내가 테스트입니다 데이터는 ...

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<?php 
    header('Content-Type: text/html; charset=utf-8'); 
    mb_internal_encoding('UTF-8'); 
    mb_http_output('UTF-8'); 
?> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<html> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<head> 
    <style type="text/css"> 
    body 
    { 
     margin: 0; 
     padding: 0; 
     background-color:#D6F5F5; 
     text-align:center; 
    } 
    .top-bar 
     { 
      width: 100%; 
      height: auto; 
      text-align: center; 
      background-color:#FFF; 
      border-bottom: 1px solid #000; 
      margin-bottom: 20px; 
     } 
    .inside-top-bar 
     { 
      margin-top: 5px; 
      margin-bottom: 5px; 
     } 
    .link 
     { 
      font-size: 18px; 
      text-decoration: none; 
      background-color: #000; 
      color: #FFF; 
      padding: 5px; 
     } 
    .link:hover 
     { 
      background-color: #9688B2; 
     } 
    </style> 

    <script> 
     (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
     (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 
     m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
     })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 

     ga('create', 'UA-60962033-1', 'auto'); 
     ga('send', 'pageview'); 

    </script> 
</head> 

<body> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <div class="top-bar"> 
     <div class="inside-top-bar"> 
      <a href="" width="500px"></a> 
      <br><br> 
      <a href="../insert.php" class="link">&larr; Back to main Website</a> 
     </div> 

    </div> 
    <h1> CSV Should be in Below Format:</h1><b>Full Name:&nbsp;Employee Number:&nbsp;Department:&nbsp;Email:</b> 
    <div style="border:1px dashed #333333; width:300px; margin:0 auto; padding:10px;"> 

    <form name="import" method="post" enctype="multipart/form-data"> 
     <input type="file" name="file" /><br /> 
     <input type="submit" name="submit" value="Submit" /> 
    </form> 
<?php 
    include ("connect.php"); 

    if(isset($_POST["submit"])) 
    { 
     $file = $_FILES['file']['tmp_name']; 
     $handle = fopen($file, "r"); 
     $c = 0; 
     setlocale(LC_ALL, 'ar_AE.utf8'); 
     while(($filesop = fgetcsv($handle, 1000, ",")) !== false) 
     { 
      $name = $filesop[0]; 
      $emp_number = $filesop[1]; 
      $department = $filesop[2]; 
      $email = $filesop[3]; 
      //$name = $filesop[0]; 
      //$email = $filesop[1]; 
      mysqli_query($link,"SET character_set_client=utf8"); 
      $sql = $link->query("INSERT INTO employees (first_name, emp_number,department,email) VALUES ('$name','$emp_number','$department','$email')"); 
      $c = $c + 1; 
     } 

      if($sql){ 
       echo "You database has imported successfully. You have inserted ". $c ." Records"; 
       echo '<td><a href="../view.php">&larr; View Inserted Records</a></td>'; 
      }else{ 
       echo "Sorry! There is some problem."; 
      } 

    } 
?> 

    </div> 
    <hr style="margin-top:300px;" />  

    <div align="center" style="font-size:18px;"><b><a href="">&copy; ALL RIGHTS RESERVED BY DIBBA MUNICIPALITY FUJAIRAH</a></b></div> 

</body> 
</html> 

를 CSV에서

가져 오기 아래 코드를 찾아주세요 전체 이름은/first_name, 1은 직원 번호, abc는 부서이며 def는 전자 메일입니다.

+0

http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – mkaatman

+0

@mkaatman 나는 이미 사랑 ... 그들은 모두 UTF8로 설정되어 읽고 ... –

+0

문제점 데이터가 포함 된 CSV의 작은 샘플을 게시하십시오. – mkaatman

답변

1

열의 데이터 정렬이 UTF8 유니 코드인지 확인해 주시겠습니까?

당신은 mysql> show full columns from employees;

을 실행하고 있는지조차 열의 데이터 정렬이 UTF8을 수용 할 수 있도록 출력을 확인할 수 있습니다.

EDIT CSV는 UTF-8 바이트 순서 표시 (BOM)가 설정되어 있지 않으면 UTF-8 데이터를 저장할 수 없습니다.

저장중인 CSV에 UTF-8 문자가 있는지 여부를 모르겠습니다. 어쩌면 noteapd에서 CSV를 열어 문자를 올바르게 가져와야하는지 확인해야합니다.

EDIT2 난 그냥 그것을 제대로 CSV를 읽고 양식이 새로운 동의 속성이 가지고 코드를 수정했습니다.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<?php 
    ini_set('display_errors', 'On'); 
    error_reporting(-1); 
    header('Content-Type: text/html; charset=utf-8'); 
    mb_internal_encoding('UTF-8'); 
    mb_http_output('UTF-8'); 
?> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<html> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<head> 
    <style type="text/css"> 
    body 
    { 
     margin: 0; 
     padding: 0; 
     background-color:#D6F5F5; 
     text-align:center; 
    } 
    .top-bar 
     { 
      width: 100%; 
      height: auto; 
      text-align: center; 
      background-color:#FFF; 
      border-bottom: 1px solid #000; 
      margin-bottom: 20px; 
     } 
    .inside-top-bar 
     { 
      margin-top: 5px; 
      margin-bottom: 5px; 
     } 
    .link 
     { 
      font-size: 18px; 
      text-decoration: none; 
      background-color: #000; 
      color: #FFF; 
      padding: 5px; 
     } 
    .link:hover 
     { 
      background-color: #9688B2; 
     } 
    </style> 


</head> 

<body> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <div class="top-bar"> 
     <div class="inside-top-bar"> 
      <a href="" width="500px"></a> 
      <br><br> 
      <a href="#" class="link">&larr; Back to main Website</a> 
     </div> 

    </div> 
    <h1> CSV Should be in Below Format:</h1><b>Full Name:&nbsp;Employee Number:&nbsp;Department:&nbsp;Email:</b> 
    <div style="border:1px dashed #333333; width:300px; margin:0 auto; padding:10px;"> 

    <form name="import" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> 
     <input type="file" name="file" /><br /> 
     <input type="submit" name="submit" value="Submit" /> 
    </form> 
<?php 
    $link = mysqli_connect('dbhost', 'dbuser', 'dbpass', 'test_arabic'); 

    if (!$link) { 
     echo "Error: Unable to connect to MySQL." . PHP_EOL; 
     echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; 
     echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; 
     exit; 
    } 

    if(isset($_POST["submit"])) 
    { 
     $file = $_FILES['file']['tmp_name']; 
     $handle = fopen($file, "r"); 
     $c = 0; 
     setlocale(LC_ALL, 'ar_AE.utf8'); 
     while(($filesop = fgetcsv($handle, 1000, ";")) !== false) 
     { 

      $name = $emp_number = $department = $email = ''; 
      $filesop2 = explode(',', $filesop[0]); 
      $name = $filesop2['0']; 
      $emp_number = $filesop2['1']; 
      $department = $filesop2['2']; 
      $email = $filesop2['3']; 

      mysqli_query($link, "SET NAMES utf8"); 
      mysqli_query($link, "set characer set utf8"); 


      $sql = $link->query("INSERT INTO employees (first_name, emp_number,department,email) VALUES ('".$name."','".$emp_number."','".$department."','".$email."')"); 
      $c = $c + 1; 
     } 

      if($sql){ 
       echo "You database has imported successfully. You have inserted ". $c ." Records"; 
       echo '<td><a href="../view.php">&larr; View Inserted Records</a></td>'; 
      }else{ 
       echo "Sorry! There is some problem. <br>".$link->error; 
      } 

    } 
?> 

    </div> 
    <hr style="margin-top:300px;" />  

    <div align="center" style="font-size:18px;"><b><a href="">&copy; ALL RIGHTS RESERVED BY DIBBA MUNICIPALITY FUJAIRAH</a></b></div> 

</body> 
</html> 
+0

예 ... 보여줍니다 나 utf8_unicode_ci 모두에 대해 ... –

+0

@UAE ADVERTISER : 도움이 될지 모르겠지만 다른 게시물의 요청에 답하기 위해 [https://pastebin.com] 코드를 테스트했습니다./hqYCh1pC). 나는 DB 테이블을 'utf8_unicode_ci'로 만들었고 csv는 'utf8 unicode'(BOO) (OpenOffice/Excel/Notepad를 사용하여 읽음)로 생성되고 저장되었습니다. 결과는 ok -> 여는 + CSV 읽기 및 삽입 + 가져 오기 + 표시는 괜찮습니다.나는이 문제에 관해서 여기 저기에 많은 글을 읽었으며, 나는 막혀있다. 나는 (DB와 CSV) 모든 설정을 다시 확인하고 * 진짜 * PHP 전문가를 위해기도하라. :) – OldPadawan

+0

@UAEADVERTISER mysql 커맨드 라인에서이 특정 문제 데이터를 선택하면 올바르게 표시됩니까? 우리는 적어도 데이터가 db로 들어가는 지 또는 올바르게 표시되지 않는지를 판단 할 수 있습니다. – mkaatman

관련 문제