如何在php中正确显示另一种语言的mysql表数据 [英] How to display a mysql table data in another language properly in php

查看:65
本文介绍了如何在php中正确显示另一种语言的mysql表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mySQL表,该表的其中一列中的数据使用的是英语以外的其他语言.(波斯语) 当我在表中输入数据时,它会正确显示,但是当我想在php文件中显示数据时,它显示如下: 那么,我应该怎么做才能以正确的形式显示数据?

i have a mySQL table which the data in one of its columns is in a language other than English.(Persian) when i input data in the table , it is shown properly but when i want to show the data in a php file it displays like this:???? well,what should i do in order to show the data in its correct form?

推荐答案

由于我已经使用非英语"字符进行了大量工作,因此正确显示和存储这些字符需要做一些事情.

As I've worked a lot with "non-english" characters, several things are required for proper display and storage of those characters.

没有特殊顺序(因为我不知道哪种字符集最适合波斯语,我将使用UTF-8,如果不同,则只使用所需的字符集即可):

In no particular order (as I don't know what charset is best suited for Persian, I'll use UTF-8, if it's different, you just use the one you need):

通过在PHP header('Content-type: text/html; charset=utf-8');中设置适当的标头或在html中设置meta标记,来告诉浏览器您正在使用的字符集,如下所示:<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Tell your browser what charset you are using, either by setting the proper header from PHP header('Content-type: text/html; charset=utf-8'); or set the meta tag in your html like so: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

在数据库中,避免在列/表中混合使用不同的排序规则和字符集.我总是将数据库,表和列设置为utf8_general_ci,这对于我的需求一直有效(英语,德语,塞尔维亚语,匈牙利语等语言).

In the database avoid mixing different collations and charsets in the columns/tables. I always set the database, the tables and the columns to utf8_general_ci which for my needs work all the time (languages like English, German, Serbian, Hungarian...).

如Jan所说,请阅读 http://dev. mysql.com/doc/refman/5.0/en/charset-connection.html 您很可能需要在连接到数据库后立即执行类似SET NAMES utf8的查询.

As Jan said, read http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html You'll most likely need to execute query something like SET NAMES utf8 right after connecting to the database.

所有这些都应确保正确显示unicode字符.但是,还有另一件事可以覆盖所有这一切-Web服务器. Apache(不了解其他服务器)具有AddDefaultCharset指令.在大多数设置中,将其保留为关闭",但是我确实遇到了将默认字符集设置为latin1的设置,因此覆盖了我的所有字符集设置.如果已设置,则在httpd.conf(或类似的配置文件)中进行设置.如果您有权访问它,我建议将其设置为关".如果不这样做,则可以在Webroot中使用.htaccess覆盖全局值,例如:AddDefaultCharset utf-8

All this should ensure the proper displaying of unicode characters. However, there is one more thing that can override all this - the web server. Apache (don't know about the other servers) has a AddDefaultCharset directive. On most setups this is left as Off, but I did came across setups where the default charset was set to latin1, thus overriding all my charset settings. If this is set, it is set in the httpd.conf (or similar configuration file). If you have access to it, I recommend setting it to Off. If you don't, then you can override the global value with .htaccess placed in your webroot, with something like: AddDefaultCharset utf-8

这篇关于如何在php中正确显示另一种语言的mysql表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆