gpt4 book ai didi

php - 将 CSV 文件导入 MySQL 数据库时出现无效的 UTF-8 字符串

转载 作者:可可西里 更新时间:2023-11-01 08:25:55 24 4
gpt4 key购买 nike

我正在尝试使用以下代码将 CSV 导入我的 MySQL 数据库:

我从帖子中获取 CSV 文件。

<?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·licitudAccés 这样的词,我收到了这个错误:

Error: Invalid utf8 character string: 'Sol\xB7licitant'

你们能帮帮我吗?

最佳答案

不知道这是否适用于这种情况,但是当我有显示错误重音的文本文件时,我只是使用 UltraEditNotepad++ 将 Enconde 更改为 UTF -8

enter image description here

关于php - 将 CSV 文件导入 MySQL 数据库时出现无效的 UTF-8 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35775687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com