META 值 charset=UTF-8 防止 UTF-8 字符显示

发布于 2024-09-16 06:06:39 字数 551 浏览 2 评论 0原文

我制作了一个测试程序,它基本上只是一个可以输入字符的文本区域,当我单击“提交”时,字符将被写入 MySQL 测试表(使用 PHP)。

测试表的排序规则是UTF-8。

如果我想将 éú 写入它可以正常写入的数据库,该脚本可以正常工作。但是,如果我将以下元语句添加到页面的 区域:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

...字符开始变得混乱。

我的理论是,服务器强加了一些运行良好的编码,但是当我添加 UTF-8 指令时,它会覆盖此服务器编码,并且此 UTF-* 编码不包含诸如 éú。 但我认为 UTF-8 编码了所有(克林贡语等)字符。

基本上我的程序可以工作,但我想知道为什么当我添加指令时它不起作用。 我想我错过了一些东西。

非常感谢任何帮助/教导。

提前致谢。

I've made a test program that is basically just a textarea that I can enter characters into and when I click submit the characters are written to a MySQL test table (using PHP).

The test table is collation is UTF-8.

The script works fine if I want to write a é or ú to the database it writes fine. But then if I add the following meta statement to the <head> area of my page:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

...the characters start becoming scrambled.

My theory is that the server is imposing some encoding that works well, but when I add the UTF-8 directive it overrides this server encoding and that this UTF-* encoding doesn't include the characters such as é and ú.
But I thought that UTF-8 encoded all (bar Klingon etc) characters.

Basically my program works but I want to know why when I add the directive it doesn't.
I think I'm missing something.

Any help/teaching most appreciated.

Thanks in advance.

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

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

发布评论

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

评论(1

感情旳空白 2024-09-23 06:06:39

首先,PHP 通常不处理 Unicode 字符集或 UTF-8 字符编码。除了(小心使用)mb_... 函数之外,它仅将字符串视为二进制数据。

其次,您需要告诉 MySQL 客户端库您正在使用什么字符集/编码。 “SET NAMES”SQL 命令完成这项工作,不同的 MySQL 客户端(mysql、mysqli 等)以不同的方式提供对其的访问,例如 http://www.php.net/manual/en/mysqli.set-charset.php

您的浏览器和 MySQL 客户端可能是两者都默认为 latin1,并且巧合地匹配。 MySQL 然后知道将 latin1 二进制数据转换为 UTF-8。当您将浏览器字符集/编码设置为 UTF-8 时,MySQL 客户端会将 UTF-8 数据解释为 latin1,并错误地对其进行转码。

因此,解决方案是将 MySQL 客户端设置为与浏览器对 PHP 的输入相匹配的字符集。

另请注意,表排序规则与表字符集不同 - 排序规则是指字符串的比较和排序方式。令人困惑的事情,希望这有帮助!

Firstly, PHP generally doesn't handle the Unicode character set or UTF-8 character encoding. With the exception of (careful use of) mb_... functions, it just treats strings as binary data.

Secondly, you need to tell the MySQL client library what character set / encoding you're working with. The 'SET NAMES' SQL command does the job, and different MySQL clients (mysql, mysqli etc..) provide access to it in different ways, e.g. http://www.php.net/manual/en/mysqli.set-charset.php

Your browser, and MySQL client, are probably both defaulting to latin1, and coincidentally matching. MySQL then knows to convert the latin1 binary data into UTF-8. When you set the browser charset/encoding to UTF-8, the MySQL client is interpreting that UTF-8 data as latin1, and incorrectly transcoding it.

So the solution is to set the MySQL client to a charset matching the input to PHP from the browser.

Note also that table collation isn't the same as table character set - collation refers to how strings are compared and sorted. Confusing stuff, hope this helps!

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