Saturday, January 12, 2013

PHPActiveRecord Fix to Support Foreign Characters Charset

Problem: 
While developing a multilingual application for a client using PHPActiveRecord, I noticed that foreign characters were not getting read properly from the database due to an incorrect charset.

Solution:
The problem is two fold. First, the database connection used needs to be set to have a UTF8 charset. Second, the documentation for UTF8 charset support for PHPActiveRecord is wrong.

From PHPActiveRecord documentation:
1 $config->set_connections(array(
2   'development' => 'mysql://user:pass@localhost/mydb;charset=utf8')
3 );


The correct version / the fix:
1 $config->set_connections(array(
2   'development' => 'mysql://user:pass@localhost/mydb?charset=utf8')
3 );
Notice that the end has a ? before charset and NOT a semi-colon. This is because this path is parsed using PHP parse_url which doesn't recognize the content following the semi-colon as a query.

No comments: