Jump to content

[SOLVED] storing Japanase Characters in MySQL database


Recommended Posts

You'll probably need to collation to be either UTF-8 or UTF-16.

 

 

I'm not sure if UTF-8 can hold Japanese characters.  I'm thinking no, but not sure.

 

 

Edit:

 

 

Oh, UTF-8 can hold Japanese characters.  So actually, you shouldn't need to change anything.

 

 

Hrmm actually, I was thinking charset, not collation...

 

*Starts googling*

I have the for these fields collation set to UTF-8_general_ci. The text when submitted into the Database displays in MySQL as 天平七年éšæˆ‘æœä½¿å¸°æœ

 

I can copy and paste the Japanese characters and have them display in MySQL ok.

So it displays fine when not through your webpage?  Is the browser reading your page as UTF8?

 

 

http://dev.mysql.com/doc/refman/5.1/en/faqs-cjk.html

 

Might be useful by the way.  I'm actually not sure if UTF-8 is the best choice...

thanks, yes if I insert the characters directly from PHPmyAdmin they work fine, but when submitted via the application which uses PHP they become jumbled as mentioned above. Maybe I need to do something with the PHP submit code?

 

I'll read over the documentation you sent also.

Yes, it does sound like you need to do stuff with the PHP code.

 

 

First off, you need to tell the browser that the page is UTF-8.

 

That's simply:

 

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

 

 

Then you need to make sure your connection to MySQL is in UTF-8:

 

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

 

 

The following is example code that I just tested this with:

 

<?php

//technically not necessary since I could just pass the encoding to mb_strlen, but oh well.
mb_internal_encoding("UTF-8");

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

$conn = mysql_connect("localhost", "root", "root");
mysql_select_db("test", $conn);
mysql_query("SET NAMES 'utf8';");

if(isset($_POST['str'])) {
if(mb_strlen($_POST['str']) != 0) {
	mysql_query("INSERT INTO japanese (str) VALUES ('".mysql_real_escape_string($_POST['str'])."');");
	header("Location: {$_SERVER['PHP_SELF']}");
	exit;
}
}

$q = mysql_query("SELECT str FROM japanese");

while($r = mysql_fetch_assoc($q)) {
echo $r['str'] . "<br>";
}
?>
<br><br>
<form action="" method="POST">
String: <input type="text" name="str" value=""><br>
<input type="submit" value="Insert">
</form>

 

 

Edit:  Oh, the table structure in that example is just:

 

CREATE TABLE japanese ( str varchar(255) ) DEFAULT CHARSET=utf8;

you also have to tell your browser that it is supposed to display japanese in the META tag

set the encode to utf

 

I once found a great guide about developing utf websites.

will try to find it again when I am at home and post the links here

 

on stackoverflow there is this topic that will help you out

http://stackoverflow.com/questions/1198701/storing-and-displaying-unicode-string-using-php-and-mysql

he is storing hindu but it will be the same for japanese.

 

if you ever plan to send emails in japanese to cellphones in japan and find how to make it work it would be really nice to PM me.

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.