2
4

3 回答 3

4

I notice that you're running this query... mysql_query('SET CHARACTER SET utf8');
Try changing that to this...

mysql_query("SET NAMES 'utf8'");

That should ensure that the connection is UTF-8.

Also try going through the list of items on this article... http://blog.loftdigital.com/blog/php-utf-8-cheatsheet
This lists the steps that are needed to make sure you're using UTF-8 from front to back in your site/application but in summary:

  • Check you've got PHP's mbstring extension and you have mb_internal_encoding('UTF-8'); set in your script.
  • Make sure you're running this MySQL query after connecting to your database mysql_query("SET NAMES 'utf8'"); which ensures the connection is UTF-8.
  • Set the HTTP header of your output... header('Content-type: text/html; charset=UTF-8');. This doesn't seem to be needed if you've set mb_internal_encoding() above but useful for debugging
  • Make sure the output encoding of your HTML page is set... <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
于 2012-09-24T15:06:36.677 回答
1

try to do a select with a charset collate and use utf8_bin like this:

SELECT k COLLATE utf8_bin AS k1
FROM t1
ORDER BY k1;

if it works for you, you can change the collate for a column by using alter table, here is an example I used today for one of my dbs: (you could do this also with phpMyAdmin easily)

ALTER TABLE  `users` 
CHANGE  `name`  `name` VARCHAR( 255 ) 
CHARACTER SET utf8 COLLATE utf8_bin NOT NULL

but make a backup first!

于 2012-07-14T15:10:23.900 回答
0

I had the same problem and I fixed with:

mysql>charset utf8mb4

I read somewhere that utf8 is broken in mysql and utf8mb4 must be used instead.

于 2020-08-28T05:19:02.107 回答