Skip to content

Instantly share code, notes, and snippets.

@peterdalle
Last active July 15, 2018 18:11
Show Gist options
  • Save peterdalle/049bf8c01f398ce3207f to your computer and use it in GitHub Desktop.
Save peterdalle/049bf8c01f398ce3207f to your computer and use it in GitHub Desktop.
Convert IP number to IP address in MySQL
# http://lite.ip2location.com/
# http://stackoverflow.com/questions/3650006/get-country-of-ip-address-with-php
# IP Number = 16777216*w + 65536*x + 256*y + z
# where IP Address = w.x.y.z
#
# To reverse IP number to IP address,
# w = int ( IP Number / 16777216 ) % 256
# x = int ( IP Number / 65536 ) % 256
# y = int ( IP Number / 256 ) % 256
# z = int ( IP Number ) % 256
SELECT
concat(floor((ip_from / 16777216) % 256), '.', floor((ip_from / 65536) % 256), '.', floor((ip_from / 256) % 256), '.', floor((ip_from) % 256)) AS fromip,
concat(floor((ip_to / 16777216) % 256), '.', floor((ip_to / 65536) % 256), '.', floor((ip_to / 256) % 256), '.', floor((ip_to) % 256)) AS toip
FROM ip2location.ip2location_db1 WHERE country_name = 'Pakistan';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment