使用PHP将latin1_swedish_ci转换为utf8 [英] Converting latin1_swedish_ci to utf8 with PHP

查看:843
本文介绍了使用PHP将latin1_swedish_ci转换为utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中填充了¥¢¥Dhaka♥¥¢â€™¥ ♥•●♥Dhaka♥•●♥)因为我没有在创建数据库时指定排序规则。

现在我要修复它。我不能从我从第一个地方拿到的数据再次获取数据。所以我想是否可能获取php脚本中的数据并将其转换为正确的字符。

我已将数据库和字段的排序规则更改为 utf8_general_ci ..

I have a database filled with values like ♥•â—♥ Dhaka ♥•â—♥ (Which should be ♥•●♥ Dhaka ♥•●♥) as I didnt specify the collation while creating the database.
Now I want to Fix it. I cannot fetch the data again from where I got it from at the first place. So I was thinking if it might be possible to fetch the data in a php script and convert it to the correct characters.
I've changed the collation of the database and the fields to utf8_general_ci..

推荐答案

排序规则与字符集不同。排序规则仅用于排序和比较文本(这就是为什么有一个语言术语)。实际的字符集可能不同。

The collation is NOT the same as the character set. The collation is only used for sorting and comparison of text (that's why there's a language term in there). The actual character set may be different.

最常见的失败不是在数据库中,而是在PHP和MySQL之间的连接。连接的默认字符集通常是ISO-8859-1。你需要改变在connectiong之后做的第一件事,使用SQL查询 SET NAMES'utf-8'; mysql_set_charset 函数。

The most common failure is not in the database but rather in the connection between PHP and MySQL. The default charset for the connection is usually ISO-8859-1. You need to change that the first thing you do after connectiong, using either the SQL query SET NAMES 'utf-8'; or the mysql_set_charset function.

还要检查表的字符集。如果您没有指定UTF-8开头(这也不是排序规则),这也可能是错误的。但是确保在更改任何东西之前进行备份。 MySQL将尝试从前一个字符集转换字符集,因此如果您在ISO-8859-1表中实际保存了UTF-8数据,则可能需要从备份中重新加载数据。

Also check the character set of your tables. This may be wrong as well if you've not specified UTF-8 to begin with (again: this is not the same as the collation). But make sure to take a backup before changing anything here. MySQL will try to convert the charset from the previous one, so you may need to reload the data from backup if you have actually saved UTF-8 data in ISO-8859-1 tables.

这篇关于使用PHP将latin1_swedish_ci转换为utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆