0

Hi I am developing a mobile app using phonegap and I am querying the MySQL database through ajax (jsonp). However I have an issue when special characters are returned as they are displayed as "?" instead for example Ż.

At the moment in my PHP I have added this, however it did not do the trick: header('content-type: application/json; charset=UTF-8');

Is anyone aware of any other charset that can be used which includes special characters like the above?

user1809790
  • 1,349
  • 5
  • 24
  • 53

3 Answers3

2

First thing is first

a) Fix the db tables Make sure that tables defined with proper character set e.g

CREATE TABLE `types` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

b) After connection to db ensure following things

SET NAMES 'utf8';

[I also run following ]

 SET  character_set_client  ='utf8',
 character_set_connection ='utf8',
 character_set_database  ='utf8',
 character_set_results   ='utf8',
 character_set_server    ='utf8',
 collation_connection    ='utf8_general_ci',
 collation_database      ='utf8_general_ci',
 collation_server        ='utf8_general_ci'

c) Finally set proper content type for the html page

hope this will help you

Community
  • 1
  • 1
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • since I have a PDO, for part (b) of your answer, would I set the characterset as follows? $options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET character_set_client = utf8, character_set_connection = utf8, character_set_database = utf8, character_set_results = utf8, character_set_server = utf8, collation_connection = utf8_general_ci, collation_database = utf8_general_ci, collation_server = utf8_general_ci', ); $db = new PDO($dsn, $username, $password, $options); – user1809790 Dec 27 '12 at 09:22
  • I never used PDO ,and that init command seems to have bug with php 5.3 http://stackoverflow.com/questions/2424343/undefined-class-constant-mysql-attr-init-command-with-pdo . Therefore, your best bet is to run : http://stackoverflow.com/questions/4978481/pdoexec-or-pdoquery – sakhunzai Dec 28 '12 at 07:31
0

You're working with a MySQL database? So try to set a utf8 charset to the database connection like:

$conn = mysql_connect('localhost','user1','pass1',TRUE); 
mysql_set_charset('utf8',$conn); 

Or try UTF-8 encoding

string utf8_encode ( string $data )

Parameters:

data
An ISO-8859-1 string.

Return Values:

Returns the UTF-8 translation of data.
Blazer
  • 14,259
  • 3
  • 30
  • 53
0

According to the JSON implementation standards, all JSON data must be encoded in UTF format, the default format being UTF-8. But you can always use other UTF formats, such as UTF-32BE, UTF-16BE, UTF-32LE, UTF-16LE.

For detailed standards and information, visit ietf standard.

SHANK
  • 2,978
  • 23
  • 31