PHP显示汉字:SET NAMES'utf8'不起作用 [英] PHP displaying Chinese characters: SET NAMES 'utf8' not working

查看:353
本文介绍了PHP显示汉字:SET NAMES'utf8'不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用现有的数据库,但是无法在其中显示汉字.该数据库实际上首先是一个MS Access文件,我通过一个程序将其转换为mysql.无论如何,很多行中都有中文字符,我无法在任何浏览器中正确显示它们.

否则,我可以很好地显示中文字符,如果我使用phpmyadmin查看表,我也可以看到它们.我四处寻找解决此问题的方法,在我看来,通常的解决方法是执行"SET NAMES'utf8'"查询,但这只会将显示的字符从问号更改为其他奇怪的符号.

如果我在phpmyadmin中查看,数据库和所有表的排序规则都是utf8_general_ci.

有什么想法吗?

解决方案

对于MySQL DB,这可以解决问题:

$dbh = mysql_connect($hostname, $username, $password);    
mysql_select_db($db, $dbh);    
mysql_set_charset('utf8', $dbh);

PDO解决方案:

$dbh = new PDO('mysql:host=$hostname;dbname=$db;charset=UTF-8', $username, $password);

I'm trying to work with a database that I have, but I can't display Chinese characters in it. The database was actually a MS Access file first, that I converted into mysql with a program. Anyway, many rows have Chinese characters in them and I can't get them to display properly in any browser.

I can display Chinese characters just fine otherwise, and I can also see them if I use phpmyadmin to look at the tables. I searched around for a solution to this problem and it seems to me that the usual fix is to do the "SET NAMES 'utf8'" query, but this only changed the displayed characters from question marks to other, weird, symbols.

If I look in phpmyadmin collation is utf8_general_ci for the database and all the tables.

Any ideas?

解决方案

For MySQL DB, this solves the problem:

$dbh = mysql_connect($hostname, $username, $password);    
mysql_select_db($db, $dbh);    
mysql_set_charset('utf8', $dbh);

PDO solution:

$dbh = new PDO('mysql:host=$hostname;dbname=$db;charset=UTF-8', $username, $password);

这篇关于PHP显示汉字:SET NAMES'utf8'不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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