views:

38

answers:

2

I'm fetching the JSON timeline from twitter and parsing it through PHP. I then want to store the text in my database.

The PHP script is in UTF8, I set the header to utf8 using this code, just in case:

header('Content-type: text/html; charset=UTF-8');

The table in the database uses utf8_general_ci, ...

Not even encoding the text using utf8_encode() works. I keep getting jumbled characters, like 'autodestruição'

Does anyone know what I'm doing wrong?

A: 

Use utf8_decode() or utf8_encode() method when delivering the JSON data.

Like

header('Content-type: text/html; charset=UTF-8');
echo utf8_decode($json_string);
powtac
I don't understand: This sends out a header telling the browser that the following content is UTF-8, but then sends ISO-8859-1 encoded data? What for?
Pekka
Maybe the data was double encoded or else scrambled
powtac
+2  A: 

You probably need to set your database connection to UTF-8 as well.

Try sending this query to the server after establishing the connection (and before INSERTing any data):

SET NAMES utf8;

If that doesn't work, please show the script you are using to fetch the data.

Pekka
That did it. But how can I set this as default in the database?
skerit
@skerit using the `--default-character-set` mySQL configuration option: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
Pekka