无法从数据库中获取要显示的正确字符

发布于 2024-08-23 01:18:02 字数 364 浏览 1 评论 0原文

我正在重新设计一个网站,但现有数据库存在问题: 数据库排序规则设置为utf8_unicode_ci,在表行中我调用的排序规则似乎设置为latin1_swedish_ci,其中存储的字符是日语(但即使在phpmyadmin中)你也会看到其他字符(我猜是因为latin1_swedish_ci)。

当我打印查询结果时,我得到一堆 ???现在使用

    mysql_query('SET NAMES utf8');
 mysql_set_charset('utf8',$conn); 

Will 输出 2009“N10Å'Ž” 2009?N10???2009“N11Å'Ž” 2009?N11???

有什么想法吗?

I'm re-designing a Web site and I have a problem with the existing data base:
The database collate is set to utf8_unicode_ci and in the table row I'm calling the collate seems to be set to latin1_swedish_ci the characters store in it are Japanese (but even in phpmyadmin) you see other characters (I guess because of the latin1_swedish_ci).

When I print the result from the query I get a bunch of ??? now using

    mysql_query('SET NAMES utf8');
 mysql_set_charset('utf8',$conn); 

Will output 2009â€N10ŒŽÂ†2009?N10???2009â€N11ŒŽÂ†2009?N11???

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2024-08-30 01:18:02

由于该表设置为使用 latin1_swedish_ci,因此无法正确存储输入的 UTF-8 数据。您需要切换该表以使用 utf8_unicode_ci 来处理未来的数据,但任何现有数据基本上都已损坏。切换排序后,您必须重新输入数据才能为现有记录获取正确的日语字符。

Because the table was set to use latin1_swedish_ci, it was unable to correctly store the UTF-8 data that was entered. You need to switch that table to use utf8_unicode_ci for data going forward, but any existing data is essentially corrupted. You would have to re-enter the data after switching the collate to get the correct Japanese characters for the existing records.

与风相奔跑 2024-08-30 01:18:02

您需要将字符集更改为utf8。不需要更改排序规则即可显示日语字符(但为了能够对文本进行排序和比较,将其更改为 utf8_general_ci 可能是一个好主意)。

You need to change the charset to utf8. The collation do not need to be changed to display japanese characters (but to be able to sort and compare texts it might be a good idea to change it to utf8_general_ci).

一口甜 2024-08-30 01:18:02

大家好,感谢您的回复,这就是发生的事情,我无法真正更改数据库中的任何内容,因为该网站的另一个版本仍然使用该数据库并且将会启动。所以我找到的解决方案如下:

案例场景:
DB设置为使用UTF8 -> (utf8_general_ci) 但字段(至少是我需要的字段设置为 latin1_swedish_ci。

解决方案:
在 mysql_connect 之后,我输入以下内容:

mysql_query("SET NAMES 'Shift_JIS'",$conn);
mysql_set_charset('Shift_JIS',$conn); 

然后在 PHP 文件中:

$titleJP = $row['titleJP'];
$titleJP = mb_convert_encoding($titleJP, "UTF-8", mb_detect_encoding($titleJP,"Shift_JIS,JIS,SJIS,eucjp-win"));

现在工作完美,字符以正确的日语显示。
我尝试了我能想到的所有其他解决方案,但没有运气(utf-8_decode/encode php 函数等..等等)

Hi all thanks for your reply's this is what happened, I couldn't really change anything in the DB since there's another version of the site that still uses that DB and will be up. So the solution I found was the following:

Case scenario:
The DB is set to use UTF8 -> (utf8_general_ci) but the field (at least the one's I needed where set to latin1_swedish_ci.

Solution:
After mysql_connect I put the following:

mysql_query("SET NAMES 'Shift_JIS'",$conn);
mysql_set_charset('Shift_JIS',$conn); 

Then in the PHP file:

$titleJP = $row['titleJP'];
$titleJP = mb_convert_encoding($titleJP, "UTF-8", mb_detect_encoding($titleJP,"Shift_JIS,JIS,SJIS,eucjp-win"));

Now that worked perfectly the characters are displayed in correct Japanese.
I tried every other solution I could think of with no luck (utf-8_decode/encode php functions, etc.. etc..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文