Question

I wrote an application using CakePHP. I set everything to use UTF-8 encoding (the database tables, the database connection in database.php, etc.), now I'm wondering whether I should encode every single text string to UTF-8 (with utf8_encode) before sending it to the database or it isn't necessary.

Thank you.

Was it helpful?

Solution

That isn't necessary.

Specifying the database connection to utf8 will handle the complexities for you. (see comments)

OTHER TIPS

Please have a look in the manual for what utf8_encode actually does. It converts ISO-8859-1 encoded text to UTF-8 encoded text. Yes, it's a horrible name for this function. If your text is not encoded in ISO-8859-1, you do not need this function. If your text is already in UTF-8, you do not need this function. In short, if you set everything to be UTF-8 from the beginning anyway, it's already UTF-8 and you do not need to do anything else.

It should be the problem of connecting php with mysql database. You can try fix this by making sure that your database server use utf-8 encoding connections by default .

You can force your cakephp application to connect to mysql with utf8 encoding by specifying encoding option in the app/config/database.php:

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'cakephp',
    'prefix' => '',
    'encoding' => 'utf8'
);

You can also force mysql to use utf8 with a query:

SET NAMES 'utf8'

Let me know if you can solve the problem. Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top