webguync Posted September 3, 2009 Share Posted September 3, 2009 Hi, Can you store Japanase characters (letters and numbers) into MySQL? If so what type and Collation would I need to use? For the english version I am using int, varchar, text, and datetime for types and utf8_general_ci for Collation. Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/ Share on other sites More sharing options...
corbin Posted September 3, 2009 Share Posted September 3, 2009 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* Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-912022 Share on other sites More sharing options...
webguync Posted September 3, 2009 Author Share Posted September 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-912044 Share on other sites More sharing options...
corbin Posted September 4, 2009 Share Posted September 4, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-912766 Share on other sites More sharing options...
webguync Posted September 5, 2009 Author Share Posted September 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-913258 Share on other sites More sharing options...
corbin Posted September 5, 2009 Share Posted September 5, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-913274 Share on other sites More sharing options...
anatak Posted September 7, 2009 Share Posted September 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-913968 Share on other sites More sharing options...
anatak Posted September 7, 2009 Share Posted September 7, 2009 found the guide I was talking about http://www.adviesenzo.nl/examples/php_mysql_charset_fix/ this is the way I do it. Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-914163 Share on other sites More sharing options...
webguync Posted September 8, 2009 Author Share Posted September 8, 2009 these ideas seem to work, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/172983-solved-storing-japanase-characters-in-mysql-database/#findComment-914799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.