utf8mb4 Encoding

Comments for “utf8mb4 Encoding”

Posted by disco_2_disco on Thursday 29th February 2024 at 00:58 GMT

I had a problem with encoding on one of my websites - lots of black triangles for the special characters.

I have fixed it by changing the $dsn line in the construct function of Model.php to this:

$dsn = 'mysql:host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname . ';charset=utf8mb4';

Was there an option I was missing elsewhere? If not, is this something that can be added?
Level One Member


User Level: Level One Member

Date Joined: 18/11/2022

Posted by DaFa on Sunday 10th March 2024 at 02:36 GMT

This way of fixing your encoding issues is fine, however. you could just ensure the database and tables are set to utf8mb4 like so:
-- Create a new database with utf8mb4 encoding
CREATE DATABASE my_database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Use the newly created database
USE my_database;

-- Create a table with utf8mb4 encoding
CREATE TABLE my_table (
    name VARCHAR(255),
    description TEXT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
In some cases, omitting the charset from the DSN connection string may rely on the default charset settings of your MySQL server or PHP environment. While this might work fine in most scenarios, explicitly specifying the charset in the connection string provides clarity and ensures consistent behavior across different environments.
Founding Member


User Level: Founding Member

Date Joined: 30/11/2018