How to save Japanese characters in a mySQL table

I had to save Japanese characters in a CSV file to a mySQL table and then display those characters on another page.

  1. I created a table in mySQL and converted the table collation and character set to utf8. I use community version of sqlyog as a mySQL client. You can change the collation and character set very easily as can be seen in the snap shot.

    You can also do this using the SQL CREATE and ALTER statements as well. It depends on which ever method you are comfortable with.
Sections highlighted in red are needed to be done so that Japanese characters can saved correctly in those columns
  1. Next I updated the PHP code and added the statement SET NAMES utf8 just after opening the connection to the mySQL database so mySQL knows that there will be data which will be in unicode format and it will need to be handled accordingly.

    You don’t need to run this statement if you are using any professional MVC framework as they cater to an international audience.
Code highlighted in red needs to be added (if not already there) so that mySQL knows it has to handle unicode data
  1. After doing steps 1 and 2 I wrote the script which read the CSV file and inserted the data into the database table and the data was saved successfully.

Hope the above helped you all.

Leave a Comment