Monday, March 29, 2010

Store an IP-address in MySQL

To store an IP-address in MySQL:

Use an INT - UNSIGNED field

INET_ATON()
mysql> SELECT INET_ATON('127.0.0.1');
-> 2130706433

INET_NTOA()
mysql> SELECT INET_NTOA(2130706433);
-> '127.0.0.1'

If you want to convert these with PHP then do:

ip2long() and long2ip()