Skip to content

Instantly share code, notes, and snippets.

@battis
Last active October 5, 2021 20:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save battis/9e1b931a3199411e2a78 to your computer and use it in GitHub Desktop.
Save battis/9e1b931a3199411e2a78 to your computer and use it in GitHub Desktop.
Trying to store/retrieve/display Unicode emoji in a MySQL database
<?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";
?>
@Thadah
Copy link

Thadah commented Dec 10, 2016

Thank you :D

@jouss3ph
Copy link

Thanks you verrrry much 👍

@KingsleyLeshey
Copy link

Thanks alot, what can be the reason for emojis to render black and white ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment