IP address

I have always stored IP addresses as strings in VARCHAR fields in MySQL.

I am currently reading “High Performance MySQL” (O’Reilly) and it mentions the advantages of storing IP addresses as integers in an INT field rather than as a string in a VARCHAR or CHAR field.

The MySQL docs specify:  “To store values generated by INET_ATON(), use an INT UNSIGNED column”.

So, with the following table:

To insert a record:

To read a record:

By the way, if you are retrieving IP Addresses using $_SERVER['REMOTE_ADDR'], consider a more advanced approach, such as borrowing Zend RemoteAddress class.

Leave a Reply