如何正确设置文件、数据库、连接等的编码?
我正在将 mysql 数据库从一个站点迁移到另一个站点。
它的编码:utf8
它的连接编码:utf8_unicode_ci
该站点的 php 文件中使用的编码:没有 BOM 的 utf-8
该站点中每个页面的标头中的编码:utf-8
该站点中的一切正常。
然后我使用 phpmyadmin 导出数据库。 它生成了一个 .sql 文件,用 utf-8 编码,当我打开它时一切都很好。
然后我将该文件复制到新站点,该站点对所有内容使用相同的编码,并将其导入。
当我通过网页在新站点中显示旧站点的数据时,它会显示损坏的字符。例如: ™ => �.
如果我将浏览器的编码从 utf-8 更改为 iso-8859-1,我会看到正确的符号。
新站点中的其他所有内容都工作正常,将内容保存到数据库并将其拉回后,我没有出现编码问题。唯一奇怪的是,当我浏览存储的数据时,phpmyadmin 显示损坏的字符。但在网站上显示内容时我没有这个问题。
我使用两个不同的程序进行导入:phpmyadmin 和 webmin。
所以我不知道这里出了什么问题,有什么想法吗?
我应该如何配置编码以免发生这种情况?
I am migrating a mysql database from one site to another.
Its encodign: utf8
Its connection encodign: utf8_unicode_ci
The encoding used in the php files of that site: utf-8 without BOM
The encoding in the headers for every page in that site: utf-8
Everything works fine in that site.
Then I exported the database using phpmyadmin.
It generated a .sql file, encoded with utf-8, and when I open it everything is fine.
Then I copied that file to the new site, which uses the same encoding for everything, and imported it.
When I show the data from the old site, in the new one, through a web page, it shows broken characters. Eg: ™ => �.
If I turn the encoding of the browser from utf-8 to iso-8859-1, I see the correct symbol.
Everything else in the new site works fine, I have no encoding problems after saving stuff to the database and pulling it back. The only strange thing is that when I browse the data stored, phpmyadmin shows broken chars. But I don't have that problem when showing the content in the website.
I did the import with two different programs: phpmyadmin and webmin.
So I have no clue about what is wrong here, any thoughts?
How should I have configured the encodings so this didn't happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许在第一个站点中您没有设置连接编码(请参阅 php 中 mysql_client_encoding() 的输出)。
如果这是问题所在,那么您以错误的格式存储了数据,并且您还使用相同的不当行为将其正确转换回来。
ps utf8_unicode_ci 不是编码,是排序规则(如何对字符串进行排序)
Maybe in the first site you didn't set the connection encoding (see output of mysql_client_encoding() in php ).
If that is the problem you stored your data in the wrong format, and you were also converting it back correctly using the same misbehaviour.
p.s. utf8_unicode_ci is not an encoding, is a collation (how to order your strings)
有一个非常好的有关 PHP 字符集和编码的常见问题解答。
There is a pretty good FAQ on charsets and encodings in PHP.
我以前在一个旧网站上发生过这种情况,它采用不同的排序规则,所以一旦你尝试将其更改为 utf8,它就会到处显示奇怪的符号。
有时phpmyadmin的后端显示奇怪的代码,但网站显示正常。
This happened me before with an old site, it was in a different collation, so once you tried to change it to utf8 it shows strange symbols all over.
Sometimes the backend in phpmyadmin shows the strange codes, but the site shows alright.