gpt4 book ai didi

来自 MySQL 数据库的 UTF-8 字符串的 PHP 输出编码问题

转载 作者:行者123 更新时间:2023-11-29 12:09:07 24 4
gpt4 key购买 nike

我知道这个问题在这里总是以一种或另一种形式出现,但我有点不知道如何解决它。我有一个运行 MySQL 的 PHP 网站,该网站将一些扩展字符显示为乱码。据我所知,从内容导入到在屏幕上显示的每一步都编码为 UTF-8。尽管如此,它仍然显示出奇怪的编码问题。这是第一个测试示例(Natural Phënåm¥na,这是故意的),mb_detect_encoding 标识为 UTF-8,我只能使用 utf8_decode 才能正确显示:

no utf8_decode: Natural Phënåm¥na
utf8_decode: Natural Phënåm¥na

第二个例子,它甚至从未正确解码(应该是 ümlaut 和“打印师的引号”(故意添加的扩展字符,作为测试:

no utf8_decode: This pürson from “Vancouver, Canadaâ€
utf8_decode: This pürson from �??Vancouver, Canada�?�

我最初的想法是它是双重编码的,但我认为事实并非如此。当我在命令行上执行查询时,MySQL 中的所有内容都显示正确。

以下是我调查过的所有内容的概要:

  • 导入的内容已验证为 UTF-8,通过 UTF-8 连接到 MySQL 导入
  • MySQL 数据库、表、列均为 UTF-8、utf_unicode_*
  • MySQL 中的character_set_client 等变量在 Amazon RDS 上设置为 utf8
  • PHP PDO 连接为 UTF-8,NAME 设置为 UTF-8
  • PHP header 字符集和 HTML 元字符集均为 UTF-8
  • mb_detect_encoding 为两个字符串返回 UTF-8

经过几个小时的故障排除后,我有点不知所措。一时兴起,我什至尝试将 HTML header /元和 PHP header 设置为 ISO-8559-1,但这也不起作用。

我最近花了一段时间与 Amazon RDS 进行斗争以获取正确的变量设置,但除此之外我没有任何想法。

mysql> show variables like '%character%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | utf8 |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /rdsdbbin/mysql-5.5.40.R1/share/charsets/ |
+--------------------------+-------------------------------------------+

所以我想知道我是否遗漏了哪些步骤?有什么明显的吗?提前致谢。

更新

这是我的 PHP 输出脚本,用于进一步说明我提到的“输出”:

<?php header("Content-type: text/html; charset=utf-8"); ?>
<html>
<header>
<meta charset="utf-8" />
<title>My test</title>
</header>
<body>
<?php


try {
$dbh = new PDO("mysql:host=localhost;dbname=database",
"user", "password", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
catch(PDOException $e) {
echo $e->getMessage();
}

$sth = $dbh->prepare("my select statement");
$sth->execute();
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);


foreach ($rows as $row) {
echo mb_detect_encoding($row['name']);
echo "<br>no utf8 decode: ". $row['name'] . "<br>\n";
echo "single utf8 decode: ". utf8_decode($row['name']) . "<br>\n";
echo "no utf8 decode: ". $row['description'] . "<br>\n";
echo "single utf8 decode: ". (utf8_decode($row['description'])) . "<br>\n";
}

?>
</body>
</html>

更新#2我还尝试直接从 PHP echo 和直接静态 HTML 将这些相同的字符输出到浏览器中,并且字符显示得很好。

echo "“test ü ö”<br>"; ?>
<p>“test ü ö”</p>

最佳答案

您不应更改所有 character_set% 字段,仅更改受 SET NAMES utf8; 影响的三个字段。

不要使用utf8_encode或解码。

您可能在存储时搞砸了。

这似乎可以恢复字符,但这不是一个可行的解决方案:

CONVERT(CAST(CONVERT('pürson from “Vancouver, Canadaâ€' USING latin1)
AS BINARY)
USING utf8)
--> 'pürson from “Vancouver, Canada - spec',

为了弄清楚做了什么,请提供

SELECT col, HEX(col) FROM tbl WHERE ...

对于某些未正确渲染的单元格。

关于来自 MySQL 数据库的 UTF-8 字符串的 PHP 输出编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992118/

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