Special Characters Are Not Stored In Database

I get data trough a POST request. The data is url encoded by the sender and is decoded automatically by PHP.

When I store this data in the database, special characters get lost. "België" is stored as "Belgi".

This shows "België" in the browser:


echo $_POST['country']; 

This returns "UTF-8":


mb_detect_encoding($_POST['country']); 

This also returns "UTF-8":




$model->attributes = $_POST;

$model->save();

mb_detect_encoding($model->country);



Character encoding of the database is set to UTF-8, encoding of the column is also set to UTF-8. Charset of the database is set to utf8 in Yii.

Setting the initSQLs property of the database in Yii to array(‘set names utf8’) doesn’t help.

Setting ini_set(‘default_charset’, ‘UTF-8’); in index.php doesn’t help.

ALTER TABLE table DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; doesn’t help.

Update: See this post.

Hi,

assmuning that you are using MySQL - Have you tried to add "charset=utf8" to your connectionString, like this:


'connectionString' => 'mysql:host=localhost;port=3306;dbname=db;charset=utf8'

(I am assuming you’ve set the property CDbConnection::charset to UTF8 - which might not work)

I tried adding the utf8 charset to the connection string, but that didn’t help. The charset property is set to utf8.

How can I debug this? Should I look at the MySQL logs?

I found that the problem as described in my first post only appears when I post the data with curl. When I post the data with a form in Google Chrome, the application works fine.

So I compared the POST data of the Curl post with the POST data of Google Chrome.

Curl: country=Belgi%EB

Chrome: country=Belgi%C3%AB

%EB is ë and %C3%AB is ë

And solved: http://stackoverflow.com/questions/12860837/why-is-e-percent-encoded-to-eb-but-also-to-c3ab

Nice follow up and solution - thanks!