将CSV文件导入MySQL数据库时,UTF-8字符串无效 [英] Invalid UTF-8 character string on import of a CSV file into a MySQL database
本文介绍了将CSV文件导入MySQL数据库时,UTF-8字符串无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码将CSV导入我的MySQL数据库:
I'm trying to import CSV into my MySQL database using this code:
我从帖子中获取了CSV文件.
I get the CSV file from post.
<?php
//conexiones, conexiones everywhere
ini_set('display_errors', 1);
error_reporting(E_ALL);
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'password';
$database = 'test';
$table = 'csvtable';
if (!@mysql_connect($db_host, $db_user, $db_pass))
die("No se pudo establecer conexión a la base de datos");
if (!@mysql_select_db($database))
die("base de datos no existe");
if(isset($_POST['submit']))
{
//Aquí es donde seleccionamos nuestro csv
$fname = $_FILES['sel_file']['name'];
echo 'Cargando nombre del archivo: '.$fname.' <br>';
$chk_ext = explode(".",$fname);
if(strtolower(end($chk_ext)) == "csv")
{
//si es correcto, entonces damos permisos de lectura para subir
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
//Insertamos los datos con los valores...
$sql = "INSERT into csvtable(id, example, example2) values('$data[0]','$data[1]','$data[2]'";
mysql_query($sql) or die('Error: '.mysql_error());
}
//cerramos la lectura del archivo "abrir archivo" con un "cerrar archivo"
fclose($handle);
echo "Importación exitosa!";
}
else
{
//si aparece esto es posible que el archivo no tenga el formato adecuado, inclusive cuando es cvs, revisarlo para
//ver si esta separado por " , "
echo "Archivo invalido!";
}
}
?>
问题是我有类似Sol·licitud
或Accés
的单词,但出现此错误:
The problem is that I have words like Sol·licitud
or Accés
and I'm getting this error:
错误:无效的utf8字符串:"Sol \ xB7licitant"
Error: Invalid utf8 character string: 'Sol\xB7licitant'
你能帮我吗?
推荐答案
不知道这种方法是否适用于这种情况,但是当我的文本文件显示错误的重音时,我只是使用UltraEdit
或Notepad++
来将Enconde更改为UTF-8
Dont know if this work for this case, but when I have text files showing wrong accents I just use UltraEdit
or Notepad++
to change the Enconde to UTF-8
这篇关于将CSV文件导入MySQL数据库时,UTF-8字符串无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文