-
-
Save ryanbekabe/4ece97c203b5fe59be78c0a828ebe543 to your computer and use it in GitHub Desktop.
Trying to store/retrieve/display Unicode emoji in a MySQL database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// MySQL schema | |
/* | |
CREATE TABLE `test` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`text` text, | |
`blob` blob, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
*/ | |
// tell the browser we're going to be using Unicode characters | |
header('Content-type text/html; charset=UTF-8'); | |
$db = new mysqli('localhost', 'test', 'test', 'test'); | |
// http://stackoverflow.com/a/2446818/294171 -- makes no difference on my system | |
$db->query("SET character_set_client='utf8'"); | |
$db->query("SET character_set_results='utf8'"); | |
$db->query("set collation_connection='utf8_general_ci'"); | |
$statement = $db->prepare("INSERT INTO `test` (`text`) VALUES (?)"); | |
$emoji = '😀'; | |
$statement->bind_param('s', $emoji); | |
$statement->execute(); | |
echo "inserted $emoji"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment