Home:ALL Converter>Searching for an IP address stored in binary via php mysql

Searching for an IP address stored in binary via php mysql

Ask Time:2014-04-21T07:06:45         Author:Devon Bessemer

Json Formatter

I am storing IP addresses in a table in their packed binary form (4 bytes for IPv4 and 16 bytes for IPv6). My question is, is there a way to search for IP addresses via SQL?

I used php (inet_pton) to convert them to binary. I wasn't aware of any MySQL function available for this. My dilemma comes from when searching for part of an IP address. Say if the IP address is 192.168.1.101 and the user searches for 101.

Since inet_pton wouldn't work for a string/integer like 101, is there an alternative for searching for packed ip addresses? I'm open to suggestions.

Author:Devon Bessemer,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23188583/searching-for-an-ip-address-stored-in-binary-via-php-mysql
John Garrard :

If you are allowing your users to enter any valid substring for searching there is the INET6_NTOA function which will convert a packed ipv4 or v 6address into a binary string which you could then do string comparisons on. This exists in version 5.7 of mysql. ",
2014-04-20T23:42:29
yy