[英]PHP charset having issues
I don't usually have issues on charset, but this one I'm not getting where the problem is. 我通常没有关于charset的问题,但是这个我没有得到问题所在。
function categories($host, $user, $pw, $bd){
$file_handle = fopen("categories.csv", "rb");
$mysqli = new mysqli($host, $user, $pw, $bd);
$mysqli->set_charset('utf8');
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(';', $line_of_text);
$description = $parts[1];
$parent = $parts[2];
$query = $mysqli->prepare("INSERT INTO categories (`description`, parent) VALUES(?, ?)");
$query->bind_param("si", $description, $parent);
$query->execute();
}
fclose($file_handle);
}
I do have the same code for others files, like countries, etc and I have no problem with charset. 我对其他文件(如国家等)有相同的代码,我对charset没有任何问题。 As you can see I use, as always, the
$mysqli->set_charset('utf8');
正如你所看到的,我一如既往地使用
$mysqli->set_charset('utf8');
code. 码。 If I echo the
$description
variable I do get the right characters (they are mostly portuguese). 如果我回显
$description
变量,我会得到正确的字符(它们主要是葡萄牙语)。
This is what I've tried so far: 这是我到目前为止所尝试的:
$query = $mysqli->prepare("SET NAMES 'utf8'"); // inside the function
$query->execute();
header('Content-Type: text/html; charset=utf-8'); // in the top of the page
But it continues to add in the database words like: Feij?o (Feijão), Ch?s (Chás). 但它继续在数据库中添加如下词:Feij?o(Feijão),Ch?s(Chás)。
I do find this stupid because I do insert in another table, like I said, countries names with accents without any problem. 我发现这很愚蠢,因为我插入另一个表格,就像我说的那样,带有重音的国家名称没有任何问题。
Any guess? 有什么猜测?
Edit: Solved, using utf8_encode. 编辑:解决,使用utf8_encode。
$query->bind_param("si", utf8_encode($description), $parent);
The problem probably is in file "categories.csv", which could not be UTF-8... 问题可能在文件“categories.csv”中,不能是UTF-8 ...
To convert the file from "ASCII" to UTF-8 you could use 要将文件从“ASCII”转换为UTF-8,您可以使用
iconv -f WINDOWS-1252 -t UTF-8 categories.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.