使用PHP从MySQL数据库获取特殊字符 [英] Getting special characters out of a MySQL database with PHP
问题描述
我有一个包含特殊字符(例如™)的表。
I have a table that includes special characters such as ™.
这个字符可以使用phpMyAdmin和其他软件输入和查看,在PHP中输出到浏览器,我得到带有问号的钻石。
This character can be entered and viewed using phpMyAdmin and other software, but when I use a SELECT statement in PHP to output to a browser, I get the diamond with question mark in it.
表类型是MyISAM。编码是UTF-8 Unicode。整理是utf8_unicode_ci。
The table type is MyISAM. The encoding is UTF-8 Unicode. The collation is utf8_unicode_ci.
html头的第一行是
The first line of the html head is
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
我尝试在输出字符串之前使用htmlentities()函数。没有运气。
I tried using the htmlentities() function on the string before outputting it. No luck.
我也试过在任何输出之前将它添加到php中(无差别):
I also tried adding this to php before any output (no difference):
header('Content-type: text/html; charset=utf-8');
最后,我试图在初始的mysql连接下添加这个(这导致额外的奇数字符显示) :
Lastly I tried adding this right below the initial mysql connection (this resulted in additional odd characters being displayed):
$db_charset = mysql_set_charset('utf8',$db);
我错过了什么?
推荐答案
以下代码适用于我。
$sql = "SELECT * FROM chartest";
mysql_set_charset("UTF8");
$rs = mysql_query($sql);
header('Content-type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($rs)) {
echo $row['name'];
}
这篇关于使用PHP从MySQL数据库获取特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!