What is the best field type and length for storing IP addresses in a MySQL database?
What about for IPv6?
Store the ip as a INT(11) UNSIGNED, then use the INET_ATON and INET_NTOA functions to store/retrieve the ip address.
INT(11) UNSIGNED
INET_ATON
INET_NTOA
Sample code:
INSERT table(ip) VALUES (INET_ATON('192.168.0.1')); /*ip = 3232235521*/ SELECT INET_NTOA(ip) As IPAddress FROM table; /*IPAddress = 192.168.0.1*/
1.4m articles
1.4m replys
5 comments
57.0k users