Skip to content

Commit

Permalink
Use MySQL’s utf8mb4 instead of utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed May 7, 2013
1 parent dd8536a commit af52b73
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* crlf=input
# Automatically normalize line endings for all text-based files
* text=auto
7 changes: 4 additions & 3 deletions database.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Why you should use `utf8mb4` instead of `utf8`: http://mathiasbynens.be/notes/mysql-utf8mb4
CREATE TABLE `redirect` (
`slug` varchar(14) collate utf8_unicode_ci NOT NULL,
`url` varchar(620) collate utf8_unicode_ci NOT NULL,
`slug` varchar(14) collate utf8mb4_unicode_ci NOT NULL,
`url` varchar(620) collate utf8mb4_unicode_ci NOT NULL,
`date` datetime NOT NULL,
`hits` bigint(20) NOT NULL default '0',
PRIMARY KEY (`slug`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Used for the URL shortener';
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Used for the URL shortener';

INSERT INTO `redirect` VALUES ('a', 'https://github.com/mathiasbynens/php-url-shortener', NOW(), 1);
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
} else {

$db = new MySQLi(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
$db->set_charset('utf8');
$db->set_charset('utf8mb4');

$escapedSlug = $db->real_escape_string($slug);
$redirectResult = $db->query('SELECT url FROM redirect WHERE slug = "' . $escapedSlug . '"');
Expand Down
2 changes: 1 addition & 1 deletion shorten.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getNextShortURL($s) {
}

$db = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
$db->set_charset('utf8');
$db->set_charset('utf8mb4');

$url = $db->real_escape_string($url);

Expand Down

0 comments on commit af52b73

Please sign in to comment.