Skip to content

Instantly share code, notes, and snippets.

@zhooravell
Created June 22, 2021 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhooravell/8e6772ac1a1e16a95b0f425c1b550d2f to your computer and use it in GitHub Desktop.
Save zhooravell/8e6772ac1a1e16a95b0f425c1b550d2f to your computer and use it in GitHub Desktop.
MySQL IP V4 and V6
CREATE TABLE `my_ip` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ip_addr` varbinary(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `my_ip` (`ip_addr`) VALUES (INET6_ATON(?)); // For IP V6
INSERT INTO `my_ip` (`ip_addr`) VALUES (INET_ATON(?)); // For IP V4
SELECT
IF(INET_NTOA(ip_addr) IS NULL OR INET_NTOA(ip_addr) = '0.0.0.0', INET6_NTOA(ip_addr), INET_NTOA(ip_addr)) as ip
FROM my_ip WHERE 1 = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment