使用 PHP 的 UTF-8 编码问题MySQL
我将数据从 MySQL 4(它们最初设置为 latin2
编码)移至 MySQL 5,并将编码设置为 UTF-8。在 phpMyAdmin 中看起来不错,UTF-8 也可以。然而,网站上出现的不是一些字符,而是问号!网站编码也设置为UTF-8,所以不明白问题出在哪里。
PHP 和 HTML 文件也设置为 UTF-8。
我该如何解决这个问题?
I moved data from MySQL 4 (they were originally set to latin2
encoding) to MySQL 5 and set the encoding to UTF-8. It looks good in phpMyAdmin, and UTF-8 is okay. However, there are question marks instead of some characters on the website! The website encoding is also set to UTF-8, so I don’t understand where the problem is.
PHP and HTML files are also set to UTF-8.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
尝试该查询。
在应用程序中的任何查询之前
Try the query
before any query in your application.
在我的服务器上,将这些添加到我的 PHP 文件中没有任何效果:
但是,一旦我将其添加到 PHP 文件的顶部,一切都会正常运行:
注意:我在数据库中使用编码
utf8_general_ci
,但是 < code>utf8_unicode_ci 对我来说是一样的。On my server, adding these to my PHP file had no effect:
But everything worked perfectly once I added this to the top of my PHP file:
Note: I am using encoding
utf8_general_ci
in my database, bututf8_unicode_ci
works the same for me.尝试将 MySQL 连接设置为 UTF-8:
并发送显式 UTF-8 标头,以防万一您的服务器有其他默认设置:
Try setting the MySQL connection to UTF-8:
And send explicit UTF-8 headers, just in case your server has some other default settings:
您不必将 PHP 和 HTML 文件设置为 utf-8。
您只需将输出编码设置为 UTF-8,浏览器就会正确显示。
在 HTML 中:
在 PHP 中:
当您从 MySQL 表中获取 UTF-8 字符串时,除非您转换编码,否则它在浏览器输出中一直都是 UTF-8。这是浏览器解释它的方式。
You don't have to set your PHP and HTML files to utf-8.
You just have to set your output encoding to UTF-8 and the browser will display appropriately.
In HTML:
In PHP:
When you get a string that is UTF-8 from the MySQL table, it will be UTF-8 all the way to browser output unless you convert the encoding. It's the way that the browser interprets it.
当您在网站上显示 UTF-8 字符,但告诉浏览器将它们解释为 拉丁语-1(或Latin-2)你看到这种乱码:
àŸ
当您在网站上显示 Latin-1(或 Latin-2)字符,但告诉浏览器将它们解释为 UTF-8 时,您会看到问号。
所以我的猜测是,你将所有内容都切换为 UTF-8(我的意思是,你告诉数据库引擎、Web 服务器和浏览器你将使用 UTF-8),但你实际上并没有将字符串转换为 UTF-8 。
按照Darkerstar所说的去做。将转储转换为 UTF-8(Notepad++ 可以轻松做到这一点)并导入再来一次。
When you show UTF-8 characters on a website, but tell the browser to interpret them as Latin-1 (or Latin-2) you see this kind of gibberish:
ß
When you show Latin-1 (or Latin-2) characters on a website, but tell the browser to interpret them as UTF-8, you see question marks.
So my guess is that you switched everything to UTF-8 (I mean, you told the database engine, the web server and the browser you would be using UTF-8), but you didn't actually convert the strings to UTF-8.
Do what Darkerstar said. Convert your dump to UTF-8 (Notepad++ can do that easily) and import it again.
在我的“connection.php”末尾添加这一行解决了我的问题。
我的连接文件的完整代码是:
我的数据库排序规则是“utf8_general_ci”。
页面为“dreamweaver 默认 utf8”和“unicode 规范化形式=C(规范分解)”。
Adding this line at the end of my "connection.php" solved my problem.
My connection file's complete code is:
My database collation is "utf8_general_ci".
Pages are "dreamweaver default utf8" and "unicode normalisation form=C (Canonical Decomposition)".
我最近遇到了这个问题(我希望这和你遇到的问题是一样的),我尝试了很多方法,但最终有效的方法非常简单。
将转储的 SQL 文件转换为 UTF-8 格式,然后导入。
顺便说一句:我使用 Notepad++ 进行转换。
I had this problem recently (I hope it’s the same problem you are having), and I tried many ways, but at the end what worked was really simple.
Convert your dumped SQL file to UTF-8 format and then import it.
BTW: I used Notepad++ for the conversion.
将.htaccess 文件放入您的网站根目录中,内容如下:
AddDefaultCharset UTF-8
并
在连接到数据库后在 dbconfig 中设置:
Put a .htaccess file in your web-site root with content:
AddDefaultCharset UTF-8
and
in your dbconfig set after connection to the database:
在 MySQL 中将每个 SQL数据库、表和字段设置为 UTF-8 似乎还不够好。很烦人。
我最终强迫这个问题来解决编码问题:
我必须在打开数据库的每个地方使用这个:
并且这有效。最后。
It doesn't seem like setting every SQL database, table, and field to UTF-8 in MySQL is good enough. Very annoying.
I ended up forcing the issue to solve encoding problems:
I had to use this every place I open the database:
And that worked. Finally.
这是一个修复。将标头设置为
header('Content-type: text/html; charset=utf-8');
然后使用utf8_decode($content)
打印您的内容。您必须同时拥有这两个才能使其发挥作用。Here is a fix. Set the header to
header ('Content-type: text/html; charset=utf-8');
Then print your content usingutf8_decode($content)
. You must have the two to make it work.Putting:
其中 $conn 是我的 Mysqli 连接
在我的配置文件中有所帮助。
Putting:
Where $conn is my Mysqli connection
in my config file helped.