且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP显示汉字:SET NAMES'utf8'不起作用

更新时间:2023-01-31 09:41:19

对于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);