UTF-8 output with CakePHP

前端 未结 4 1494
野性不改
野性不改 2020-12-19 07:18

I\'m trying to move some Excel-Data to MySQL, but having troubles with encoding.

What I did:

  1. Data export from OpenOffice 3.1 as csv
相关标签:
4条回答
  • 2020-12-19 07:29

    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).

    Configure::write('App.encoding', 'UTF-8');
    

    Once all three parts are changed, you should have a consistent charset and hence display.

    Hope that helps any people still searching for this.

    0 讨论(0)
  • 2020-12-19 07:32

    I had a similar symptom, my solution was to add

    'encoding' => 'UTF8' 
    

    to config/database.php

    0 讨论(0)
  • 2020-12-19 07:39

    Are you setting the connection to UTF-8 with mysql?

    SET NAMES 'UTF-8'
    
    0 讨论(0)
  • 2020-12-19 07:41
    $conn1  = mysqli_connect($this->db_host, $this->db_username, $this->db_password, $this->db_name);
    

    Add this in connection

    mysqli_set_charset($conn1,"utf8mb4");

    0 讨论(0)
提交回复
热议问题