views:

93

answers:

1

Hallo all, I have a MySQL database with the following settings:
MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_ci

i have a table with a column named "softtitle", this column is coded in utf8_general_ci. The entries of this column contain chinese characters. If i run SQL through the phpMyAdmin Control pane, the chinese characters can be shown correctly. But if i run SQL through a php file, all chinese characters are shown wrongly. Here is the php file:

<?php
header("Content-Type: text/html; charset=utf8_general_ci"); 

mysql_connect("116.123.163.73","xxdd_f1","xxdd123"); // host, username, password
mysql_select_db("xxdd");

mysql_query("SET names 'utf8_general_ci'"); 

$q=mysql_query("SELECT  softtitle
FROM  dede_ext
LIMIT 0 , 30");

while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>

What is wrong here? What should i do to fix this problem? Thank you very much!

+3  A: 

You header is wrong. You're not supposed to set it to the character set of the table/database.

header("Content-Type: text/html; charset=UTF-8");

The same applies for "SET NAMES":

mysql_query("SET names 'utf8'"); 

And as a last thing, you are printing out json encoded data, your Content-type shouldn't be text/html but application/json.

halfdan
After i have changed the code like you said. Nothing displays on the webseit. What's wrong? Thank you!
tiandong
How could I, or anyone know that? Have a look at your error messages (logs?).
halfdan
Sorry, the problem caused by a missing '/' character before the comment :-)
tiandong
But still, the problem of showing chinese characters is not solved. Instead of showing "Test中文",the webseit shows me "Test\u4e2d\u6587". How can i make it display correctly?
tiandong