I want to select a string containing a micro character (µ
) and a superscript from a table and insert it into another table.
Both the source field and the destination field are formatted in utf8mb4_unicode_ci (not the whole table, just the fields)
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| type_name | varchar(30) | NO | PRI | NULL | |
| unit_of_measure | varchar(5) | NO | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
I tried using this command
INSERT INTO dest_table (type, unit_of_measure)
SELECT DISTINCT type, unit_of_measure FROM source_table;
to just insert them by selecting them, but: a SELECT from the source table gives me the correct result
SELECT unit_of_measure FROM source_table LIMIT 1
Result: µg/m³
And a select from the destination table gives me a "stripped"(?) result:
SELECT unit_of_measure FROM dest_table LIMIT 1
Result: µg/m
(expected: µg/m³
) OR a result with an unknown character:
Result: ng/m�
(expected: ng/m³
)
This is the dataset I'm using, the column I need is the "UnitaMisura" one. I'm using PDO with a local MySQL connection.
Any help is appreciated.
Edit 1:
SELECT DISTINCT unit_of_measure, HEX(unit_of_measure) FROM source_table
Result: C382C2B5672F6DC382C2B3
(µg/m³
)
SELECT DISTINCT unit_of_measure, HEX(unit_of_measure) FROM dest_table
Result: C382C2B5672F6D
(µg/m
)
FIX:
I had to set the PDO connection charset, by adding charset=utf8mb4 at the end of the connection string.
Example: new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8mb4", $db_user, $db_password)