奇怪的 MySQL 编码问题

发布于 2024-10-14 17:57:29 字数 679 浏览 7 评论 0原文

我在页面表中有一个包含阿拉伯语数据的列。

如果我这样做...

@mysql_query( "select title from pages where web_id = '$id'" );

我会得到正确的阿拉伯语数据。

但如果我再次调用同一个函数,事实上,它处于循环中,第二次它会给我带来垃圾数据。看起来像这样

"تنظيم الأوقا٠العامة اقرأ قطاع

另外,如果在该查询之前,我执行以下操作之一:

@mysql_query("SET NAMES 'utf8'");
@mysql_set_charset( 'utf8' );
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'" );

我总是得到垃圾数据,即使是在第一次调用时也是如此。这里发生了什么事?

请注意,数据库、表和字段排序规则均设置为 utf8_bin

I have a column in the pages table with arabic data.

If I do this...

@mysql_query( "select title from pages where web_id = '$id'" );

I get correct data, in arabic.

But if I call the same function again, in fact, it's in a loop, the second time it brings me garbage data. Looking something like this

"تنظيم الأوقا٠العامة اقرأ قطاع

Also, if before that query, I do either of this:

@mysql_query("SET NAMES 'utf8'");
@mysql_set_charset( 'utf8' );
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'" );

I always get garbage data, even on first call. What's happening here?

Note that database, table and field collations are set to utf8_bin

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

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

发布评论

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

评论(2

够钟 2024-10-21 17:57:29

一些随机提示:

尽量不要使用 @ 运算符。它将抑制任何错误消息。

您需要正确转义输入参数:

mysql_query("SELECT title FROM pages WHERE web_id = '" . mysql_real_escape_string($id) . "'");

您需要检查mysql_query()的返回值:

$res = mysql_query("SELECT title FROM pages WHERE web_id = '" . mysql_real_escape_string($id) . "'");
if(!$res){
    die('Could not fetch page title'); // See mysql_error() for error message
}

确保您的PHP源代码保存为UTF-8。任何像样的编辑器都允许您另存为 UTF-8 并在状态栏中显示当前编码。

检查您的表定义。 MySQL 允许设置每个表和列的编码。

使用mysql_set_charset()设置连接编码。

确保浏览器将您的页面呈现为 UTF-8。在 PHP 中,您可以使用以下命令生成适当的 HTTP 标头:

header('Content-Type: text/html; charset=utf-8');

Some random hints:

Try not use the @ operator. It will suppress any error message.

You need to escape input parameters properly:

mysql_query("SELECT title FROM pages WHERE web_id = '" . mysql_real_escape_string($id) . "'");

You need to check the return value of mysql_query():

$res = mysql_query("SELECT title FROM pages WHERE web_id = '" . mysql_real_escape_string($id) . "'");
if(!$res){
    die('Could not fetch page title'); // See mysql_error() for error message
}

Make sure your PHP source code is saved as UTF-8. Any decent editor will allow you save as UTF-8 and will display current encoding in the status bar.

Check your table definition. MySQL allows to set per table and column encodings.

Set the connetion encoding with mysql_set_charset().

Make sure the browser renders your page as UTF-8. In PHP, you can generate the appropriate HTTP header with:

header('Content-Type: text/html; charset=utf-8');
橘亓 2024-10-21 17:57:29

您应该验证您的字段是否确实包含 UTF-8 数据。即使设置为 UTF-8,您也可能错误地导入了数据或者数据可能已损坏。您可以使用以下方法查看特定的字节流:

SELECT hex(myfield) FROM mytable;

如果您导入了数据,您应该注意到数据文件可能已被解释为采用不同的编码 - 事实上甚至可能已经这样做了。您需要确保该文件被正确识别,例如:

mysql --default-character-set=utf8 database < mydbbackup.sql 

You should verify whether your field actually contains UTF-8 data. Even if it's set to UTF-8 you may have imported data incorrectly or that data might be corrupt. You can look at the specific byte stream using:

SELECT hex(myfield) FROM mytable;

If you've imported data you should note that the data file might have been interpreted as being in a different encoding - indeed it might even have been do. You'd need to ensure that the file is correctly recognised, e.g.:

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