如何用 PHP 显示 Unicode 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2446778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display Unicode data with PHP
提问by Egglabs
table 'abc' data :
tid title
1 ????????????? ?.
2 ?????? ??????
$sql=mysql_query("select title from abd where tid='1'");
$row=mysql_fetch_array($sql);
$title = $row['title'];
echo $title;
OutPut displaying like this:
输出显示如下:
????????????????
But I want to display
但我想显示
????????????? ?.
Solution
解决方案
<?php
mysql_query ("set character_set_results='utf8'");
$sql=mysql_query("select title from abd where tid='1'");
$row=mysql_fetch_array($sql);
$title = $row['title'];
echo $title;
?>
回答by antyrat
Try to set charachter encoding after mysql_connectfunction like this:
尝试在mysql_connect函数后设置字符编码,如下所示:
mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
回答by MohsenB
For new version of PHPwhich used mysqli, use this code after connect to mysql:
对于使用的新版本PHP,mysqli在连接到 mysql 后使用此代码:
mysqli_set_charset($link, 'utf8');
回答by Pascal MARTIN
Try to make sure the browser recognizes the page as Unicode.
尝试确保浏览器将页面识别为 Unicode。
Generally, this can be done by having your server send the right Content-typeHTTP header, that includes the charset you're using.
通常,这可以通过让您的服务器发送正确的Content-typeHTTP 标头来完成,其中包括您正在使用的字符集。
For instance, something like this should work :
例如,这样的事情应该工作:
header('Content-type: text/html; charset=UTF-8');
echo "????????????? ?";
If this works, and your dynamically generated page still doesn't :
如果这有效,并且您的动态生成的页面仍然无效:
- make sure your data in your MySQL database is in UTF-8 too
- this can be set for each table, or even columns, in MySQL
- and make sure you are connecting to it using UTF-8.
- 确保您的 MySQL 数据库中的数据也是 UTF-8
- 这可以在 MySQL 中为每个表甚至列设置
- 并确保您使用 UTF-8 连接到它。
Basically, all your application should use the same encoding :
基本上,您的所有应用程序都应该使用相同的编码:
- PHP files,
- Database
- HTTP server
- PHP文件,
- 数据库
- HTTP服务器
回答by Silver Light
execute query
执行查询
SET NAMES 'utf8'
right after connecting to database
连接到数据库后
回答by Mohan Kumar
<?php header('Content-type: text/html; charset=UTF-8');echo "<font face='Bamini' size='5'>" . "m.Nkhfd; Fkhh;" ."<br />";
echo "<font face='Bamini' size='10'>" . "m.Nkhfd; Fkhh;" ."<br />";
?>
or
<?php
header('Content-type: text/html; charset=UTF-8');
echo "à?μàˉ?à?3àˉà?3à??à??àˉà??à??à?′à??àˉ? à?";
?>
回答by Sarfraz
First you need to convert your mysql data format to utf-8, see this great article by oreilly:
首先,您需要将 mysql 数据格式转换为utf-8,请参阅 oreilly 的这篇很棒的文章:
.
.
.
.
After that make sure that your page's encoding type is utf-8:
之后确保您的页面的编码类型是utf-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
回答by sachu forever
While inserting the data into a MySQL database using the relevant collation (ex. utf8_general_cifor Unicode characters, i.e Hindi). Also in use the PHP predefined function utf8_decodeto display the characters in HTML.
使用相关排序规则将数据插入 MySQL 数据库时(例如utf8_general_ci,Unicode 字符,即印地语)。也在使用 PHP 预定义函数utf8_decode来显示 HTML 中的字符。

