UTF-8 output with CakePHP
I'm trying to move some Excel-Data to MySQL, but having troubles with encoding.
What I did:
- Data export from OpenOffice 3.1 as csv (utf-8 encoded)
- Import to phpMyAdmin via file upload (Table encoding: 'utf8_unicode_ci')
In phpMyAdmin's view mode, the data is displayed correctly (it is using utf-8 as charset):
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
When I try to display the Data on my webpage, I get a hash with a question mark in it.
- The language I try to get on my page: German
- MySQL client version: 5.0.32
- My OS: MAC OS X 10.5.7
- Server-Script: CakePHP v22.214.171.12466
I had a similar symptom, my solution was to add
'encoding' => 'UTF8'
I've had a similar problem to this. Most things are set correctly "out of the box", but I'd just like to point out the following things I found useful... I hit this problem while moving from dev to live:
You need to have matching Database encoding (2 items to check) as well as view encoding.
- Your DB encoding is set up in the Schema.
- Cake PHP can be manually forced to expect a type of encoding from the DB.
- Cake PHP then needs to call the correct encoding for the view as well.
The DB settings are, of course, set up in your database. In my case, using MySQL workbench, this was simply a case of right clicking the schema and selecting "Alter Schema...". From there I could select the encoding/collation I wanted.
In Cake PHP the database encoding is set in app\Config\database.php. You should ensure that the DATABASE_CONFIG array (approx line 60), has the correct encoding enabled/selected. For example:
'encoding' => 'utf8'
Finally, the view needs to select the correct version of HTML to display. This is written into your templates from the file app\Config\core.php (approx line 82).
Once all three parts are changed, you should have a consistent charset and hence display.
Hope that helps any people still searching for this.
Are you setting the connection to UTF-8 with mysql?
SET NAMES 'UTF-8'
$conn1 = mysqli_connect($this->db_host, $this->db_username, $this->db_password, $this->db_name);
Add this in connection