0

I am using restler PHP API to host a REST service. I am having a problem with handling some European characters, otherwise it is working fine.

For example I have the string "Český rozhlas 1 - Radiožurnál" in a MySQL database. When the restler API converts the data in to JSON, it is converted like this "?esk\u00fd rozhlas 1 - Radio\u009eurn\u00e1l"

Here first character is converted as a question mark.

How can I convert the data properly using the restler PHP service?

jpsasi
  • 1,905
  • 2
  • 18
  • 29

1 Answers1

1

When dealing with Unicode, we need to make sure we use utf-8 all the way

First you need to make sure MySQL database is using utf-8 encoding. You can run the following sql to make sure of that

ALTER TABLE your_table_name_here CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

Next you need to make sure MySQL spits utf-8 when talking to PHP.

You can use the following commands

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

If you are using PDO you need to use the following instead, for connecting to the database

$db = new PDO(
          'mysql:host=localhost;dbname=data_pdo_mysql', 'username', 'password',
          array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")
      );

After these changes, restler result should become

"\u010cesk\u00fd rozhlas 1 - Radio\u017eurn\u00e1l"

Which is valid JSON with complete data where unicode characters are escaped using unicode escape sequences.

Arul Kumaran
  • 983
  • 7
  • 23
  • Thanks for your response. I resolved this issue after adding the following statement. mysql_query("set character_set_results='utf8'") – jpsasi May 12 '12 at 10:42