Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Forked from battis/emoji.php
Created July 15, 2020 13:39
Show Gist options
  • Save ryanbekabe/4ece97c203b5fe59be78c0a828ebe543 to your computer and use it in GitHub Desktop.
Save ryanbekabe/4ece97c203b5fe59be78c0a828ebe543 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";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment