Using UTF-8 in Ajax, PHP, MySQL & ASP.NET

We have a project which needs to show foreign language, like Chinese, on a web page. And we are using RICO 2.0, PHP, MySQL & ASP.NET in the project. To achieve this, we decide to use UTF-8 character set and the following changes need to be done:

  • MySQL:

To store UTF-8 in a table field, you need to set column’s charset => utf8 and collate => utf8_unicode_ci

  • Web Page:

To show foreign language in a web page, the page file has to be saved as encoding UTF-8

  • PHP:

The default encoding for PHP 5 is UTF-8, so it supports foreign language. But if you use PHP to retrieve UTF-8 characters from MySQL, you must add mysql_query(‘SET NAMES utf8’); after connecting to MySQL database for setting up the connection encoding.

  • RICO:

As for RICO Live gird, Ajax response must start with <?xml version=”1.0″ encoding=”utf-8″?>

  • ASP.NET:

First make sure you have following line in you web.config
<globalization requestEncoding=”utf-8″ responseEncoding=”utf-8″ fileEncoding=”utf-8″/>
Second, keep both .aspx and code behind file save as the same encoding UTF-8
If you need to access the UTF-8 characters in MySQL database, be sure to use MySQL Connector/Net 5.0 or up with undocumented setting: charset=utf8 in connection string. We tried ODBC driver for MySQL, it doesn’t work properly with UTF-8.

2 Responses to “Using UTF-8 in Ajax, PHP, MySQL & ASP.NET”

  • It is possible to use utf8_general_ci?

  • To use Unicode characters in SQL server, nchar, nvarchar or ntext data type should be used, and a leading N for all unicode fields. e.g. insert test values (2, ‘name’, N’李宗伟’); select * from test where [value] = N’李宗伟’,

    If you are using LINQ to retrieve the data from SQL server, you don’t have to worry about ‘N’. LINQ will take care for you.

Leave a Reply

*