If you ever need to store an IP address in a MySQL database, use ip2long and store it in an int(11). I think that is the best way.
int ip2long ( string ip_address)Of course, to go back, just use long2ip. Posted by torque at June 26, 2004 10:21 PM | TrackBack
The function ip2long() generates an IPv4 Internet network address from its Internet standard format (dotted string) representation. If ip_address is invalid then -1 is returned. Note that -1 does not evaluate as FALSE in PHP.In PHP 5 ip2long() returns FALSE when the ip_address is in valid.
Or, you could use the MySQL functions INET_ATON and INET_NTOA, which basically perform the same function.
Posted by: Simon at July 30, 2004 2:57 PMGood point Simon, in fact, that is what I ended up doing. It makes a lot more sense to have MySQL to the batch conversion since it is already hard-wired to do that. A word to the audience, anytime you can compute in the database, do it!
Posted by: tim at July 30, 2004 4:53 PMI'm trying to store IP's in a simple database. Meaning, a user can input an IP and then see the database list. Using INET_ATON, how is this possible? I understand the command, but not sure how to implement it in a PHP script.
Posted by: Nir0 at December 9, 2004 11:33 AMNir0:
INSERT INTO ips VALUES(INET_ATON('127.0.0.1'));
SELECT INET_NTOA(ip) FROM ips;
Of course, you'll probably have more fields...
Posted by: Simon at February 13, 2005 7:42 AMlol :) Thanks to people like you, theres a blog entry for every programming problem conceivable. I found you via google.
Thanks for the tip
Posted by: joe at November 25, 2006 6:41 PMGood job!
Posted by: Markus at December 12, 2006 7:27 PM