gpt4 book ai didi

PHP/MySQL:修复隐式 mysqli::set_charset ('latin1' ) 连接损坏的 utf8 文本

转载 作者:行者123 更新时间:2023-11-29 01:51:55 26 4
gpt4 key购买 nike

因此,多年来,我的 PHP 应用程序一直使用默认的 latin1 字符集连接到 MySQL。尽管我将一些字段整理为 utf8_general_ci,但存储到它们中的实际数据是一些 SCSS 字符集。例如:

输入:♠ »

存储为 â™

现在,当该数据通过相同的 latin1 连接检索并显示在编码设置为 utf8 的页面上时,它会像输入时一样显示:♠ » 为什么会这样,我不是 100% 确定,但我猜这是因为无论什么字符集函数搞砸了它,都会修复它。

我想修复我的数据。如果我使用 mysqli::set_charset('utf8') 切换我的连接字符集,输出将按存储时的形式显示,即 â™ »

所以,显然我需要修复我现有的数据,然后切换我的连接字符集。

如何修复现有的 SCSS 数据?

编辑:

I've discovered a way to emulate the corruption process that is happening in a MySQL query: SELECT CAST(BINARY '♠ »' AS CHAR CHARACTER SET latin1) outputs ♠»

Perhaps if I could figure out how to perform the reverse function I could use that query to fix the existing data.

编辑 2:

I've discovered such a function: SELECT CAST(BINARY CAST('♠»' AS CHAR CHARACTER SET latin1) AS CHAR CHARACTER SET utf8) outputs ♠ »

My only concern now is what this will do to any data that already happens to be actual utf8 data, which, for some reason, I do have in my database. For example, SELECT CAST(BINARY CAST('♠ »' AS CHAR CHARACTER SET latin1) AS CHAR CHARACTER SET utf8) outputs (nothing)

最佳答案

来自 http://jonisalonen.com/2012/fixing-doubly-utf-8-encoded-text-in-mysql/ :

将可能损坏的 latin1 文本数据转换为 utf8 的自动检测功能:

DELIMITER $$

CREATE FUNCTION maybe_utf8_decode(str text charset utf8)
RETURNS text CHARSET utf8 DETERMINISTIC
BEGIN
declare str_converted text charset utf8;
declare max_error_count int default @@max_error_count;
set @@max_error_count = 0;
set str_converted = convert(binary convert(str using latin1) using utf8);
set @@max_error_count = max_error_count;
if @@warning_count > 0 then
return str;
else
return str_converted;
end if;
END$$

DELIMITER ;

用法:

update mytable set mycolumn = maybe_utf8_decode(mycolumn);

关于PHP/MySQL:修复隐式 mysqli::set_charset ('latin1' ) 连接损坏的 utf8 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261867/

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