When I save certain characters to my MariaDB database, they are being stored as a string of 2-4 characters.
For example, ™
is saved in the database as â„¢
.
From the head
of the html page that contains the form I have
<meta charset="utf-8">
When I check the headers sent to the server, it shows
Content-Type:text/html; charset=UTF-8
The PHP file handling the form data has
$mysqli->set_charset('utf8');
I've converted the database and table to UTF8, by doing:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Checking the database and table details in information_schema table I can see that the database and table character set is UTF8
and default collation name is utf8_general_ci
.
But the wrongly encoded characters are still being saved to the db.
I'm viewing the table data with terminal, so there should be no issue with the encoding on the viewing side.
What else am I missing?
== EDIT ==
In response to @rick's comments -
SHOW CREATE TABLE search_templates;
:
search_templates | CREATE TABLE `search_templates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` timestamp NULL DEFAULT NULL,
`template` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`space` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`secure` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
and SELECT title, HEX(title) FROM search_templates WHERE id = 25;
:
+-----------------------+--------------------------------------------+
| title | HEX(title) |
+-----------------------+--------------------------------------------+
| Kiddicare abc â„¢ | 4B69646469636172652061626320C3A2E2809EC2A2 |
+-----------------------+--------------------------------------------+
When passed through hex-to-text converter it shows: Kiddicare abc â„¢
.
So these characters are being saved to the DB like this, does this make the issue any clearer?
== EDIT ==
On advice from @rick (with freshly added data, as previously title column got wiped):
SELECT title, CONVERT(BINARY(CONVERT( CONVERT(UNHEX(title) USING utf8) USING latin1)) USING utf8) FROM search_templates LIMIT 3;
->
+--------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+
| title | CONVERT(BINARY(CONVERT( CONVERT(UNHEX(title) USING utf8) USING latin1)) USING utf8) |
+--------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+
| Kids' Games & Toys » Toy Shop » Toys R Us | NULL |
| Kiddicare - Pushchairs | Prams | Car Seats | Baby Equipment | Online Baby Shop | NULL |
| Kiddicare â„¢ | NULL |
+--------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+