IP address storing in MySQL database using PHP

user552828 picture user552828 · Jun 21, 2011 · Viewed 69.5k times · Source

what is the right field type for IP address in mysql? and what is the right way of storing it using PHP?

Answer

Francois Deschenes picture Francois Deschenes · Jun 21, 2011

This tutorial might help you.

The most efficient way of saving IPv4 addresses is with an INT field (not VARCHAR as you might expect). You convert them using PHP's ip2long and back using either MySQL's INET_NTOA function or PHP's long2ip function.

If you need to store IPv6, you'll want to use a BINARY field instead and PHP's inet_pton function.