奇怪的 MySQL 编码问题
我在页面表中有一个包含阿拉伯语数据的列。
如果我这样做...
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些随机提示:
尽量不要使用
@
运算符。它将抑制任何错误消息。您需要正确转义输入参数:
您需要检查
mysql_query()
的返回值:确保您的PHP源代码保存为UTF-8。任何像样的编辑器都允许您另存为 UTF-8 并在状态栏中显示当前编码。
检查您的表定义。 MySQL 允许设置每个表和列的编码。
使用
mysql_set_charset()
设置连接编码。确保浏览器将您的页面呈现为 UTF-8。在 PHP 中,您可以使用以下命令生成适当的 HTTP 标头:
Some random hints:
Try not use the
@
operator. It will suppress any error message.You need to escape input parameters properly:
You need to check the return value of
mysql_query()
: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:
您应该验证您的字段是否确实包含 UTF-8 数据。即使设置为 UTF-8,您也可能错误地导入了数据或者数据可能已损坏。您可以使用以下方法查看特定的字节流:
如果您导入了数据,您应该注意到数据文件可能已被解释为采用不同的编码 - 事实上甚至可能已经这样做了。您需要确保该文件被正确识别,例如:
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:
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.: