2016-03-23 17 views
1
ile okunan

CSV read ve MySQL insert çalışma örneği sunabilen herkes - hepsi utf8 ile? Bunu nasıl yapacağını anlayamıyorum (uzun saatler ...). MySQL harmanlama tüm utf8mb4_unicode_ci (veritabanı, tablo, sütun).PHP CSV MySQL insert (utf8)

Yardımlarınız için teşekkürler!

<?php 
header('Content-Type: text/html; charset=UTF-8'); 
mysql_connect("localhost","root",""); 
mysql_query("SET NAMES utf8"); 
mysql_select_db("test"); 

if (!empty($_FILES['csv']['size']) && $_FILES['csv']['size'] > 0){ 

    $file = $_FILES['csv']['tmp_name']; 
    utf8_encode(fgets($file)); 
    $handle = fopen($file,"r"); 

    do { 
     if ($data){ 
      $data = array_map("utf8_encode", $data); 
      mysql_query("INSERT INTO mytable (id,text) VALUES 
      (
       '".addslashes($data[0])."','".addslashes($data[1])."' 
      )");   
     } 
    } 
    while ($data = fgetcsv($handle,1000,",","'")); 
    header('Location: index.php'); die; 
} 
?> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 

<body> 
<form action="index.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
<input name="csv" type="file" id="csv" accept-charset="utf-8"/> 
<button type=submit>UPLOAD</button> 
</form>  
</body> 
</html> 

cevap

0

İşte başlıyoruz, virgülle ayrılmış değerler (CSV) biçiminde veri aktarabilirsiniz. Bu utf8 için MySQL insert ile okunan PHP CSV çalışma örneğidir.

<?php 
header('Content-Type: text/html; charset=UTF-8'); 
mysql_connect("localhost","root",""); 
mysql_select_db("test"); 

if (!empty($_FILES['csv']['size']) && $_FILES['csv']['size'] > 0){ 

     $file = $_FILES['csv']['tmp_name']; 
     $handle = fopen($file,"r"); 
     mysql_query("SET NAMES utf8mb4"); 
     mysql_set_charset('utf8mb4'); 

     do { 
      if ($data){ 
       utf8_encode($data); 
       mb_strtolower($data[0], 'UTF-8'); 
       mb_strtolower($data[1], 'UTF-8'); 

       mysql_query("INSERT INTO mytable (id,text) VALUES 
       (
        '".addslashes($data[0])."','".addslashes($data[1])."' 
       )");   
      } 
     } 
     while ($data = fgetcsv($handle,1000,",","'")); 
     header('Location: index.php'); die; 
} 
?> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 

<body> 
    <form action="index.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <input name="csv" type="file" id="csv" accept-charset="utf-8"/> 
    <button type=submit>UPLOAD</button> 
</form>  
</body> 
</html>