Best way to store IP in database?
Store the ip as a INT(11) UNSIGNED
, then use the INET_ATON
and INET_NTOA
functions to store/retrieve the ip address.
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*/
It depends on what you want to do with it, but probably the simplest way would be to represent it as a string. And this question covers how many characters would be required to handle it. (It's 45).