I am trying to insert UTF-8 characters into a MySQL table using Lucee and having no luck.
- I have the MySQL table set to use utf8mb4_unicode_ci but have also tried utf8mb4_bin.
- I have tried Apache both with and without “AddDefaultCharset UTF-8” enabled.
- The connection string to the database includes “characterEncoding=UTF-8”
- Lucee config includes UTF-8 in the charset settings.
The code I am running is as follows…
<cfset textValue = '🙋 Person Raising Hand'>
<cfdump var="#textValue#">
<cfquery name="insert">
INSERT INTO TEST_UTF8 (TestText)
VALUES ('#textValue#');
</cfquery>
The dump works fine and displays as it should but the insert returns the following error…
“Incorrect string value: ‘xF0x9Fx99x8B P…’ for column ‘TestText’ at row 1”
I have tried inserting the same string via phpmyadmin and it goes in fine suggesting that the MySQL config is okay.
2
Answers
Okay, with a little more help and reading, I needed to add "character_set_server=utf8mb4" to the MySQL config file at "/etc/mysql/my.conf". Looks like this is an issue caused by an older version of the connector.
From MySQL docs (thanks to CfSimplicity for leading me to the page)...
That 4-byte Emoji needs MySQL’s
utf8mb4
;utf8
will not do. To the outside world,utf8mb4
is calledUTF-8
(with a dash)`.The column in the table needs to say
CHARACTER SET utf8mb4
and the connection needs to say it also:Add
?useUnicode=yes&characterEncoding=UTF-8
to the JDBC URLTo use 4-byte UTF8 with Connector/J configure the MySQL server with
character_set_server=utf8mb4
. Connector/J will then use that setting as long as characterEncoding has not been set in the connection string. This is equivalent to autodetection of the character set.