Jump to content

[SOLVED] Ack, PHP returning question marks from MySQL entries for Japanese characters!


eugeniu

Recommended Posts

I have a MySQL table in which one column specifically had Japanese characters in it. But when I try to retrieve the content of an entry with Japanese words, I get question marks. For example, "ひらがな" would show up as "????". It shows up just fine when I look at it through phpMyAdmin. I tried changing the field to use utf8_general_ci, utf8_unicode_ci, sjis_japanese_ci, and ujis_japanese_ci, but none of those make a difference. This is the code I use to retrieve the entries:

 

mysql_connect("...") or die(mysql_error());
mysql_select_db("...") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM kanawords") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['kana'];
echo "</td><td>"; 
echo $row['answer1'];
echo "</td></tr>"; 
} 

echo "</table>";

 

What am I doing wrong?

If phpmyadmin display correctly the data, then your data is fine and the problem is coming from your script.

 

First set this in your database :

Charset : utf8

Collation : utf8_general_ci

(ci is for case insensitive)

 

Then you need to tell mysql what charset to use for the connextion like this (right after you connect and select the database) :

mysql_query("SET NAMES 'utf8';");
mysql_query("SET CHARACTER SET 'utf8';");

Then you need to tell the browser what charset you use like this :

 

This for the http header (it need to be put before any output even enter or space to work) On each page :

header('Content-Type: text/html; charset=utf-8');

And you need to tell the browser again in html because some browser like that :

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 

If you are using XHTML instead of HTML it will be more complex depend on how you server your XHTML as text/html or as in XML.

 

Should work fine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.