Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.