I was going to use varchar(20)
, but I was wondering what should if I should do INT and strip off the periods instead. What would be better and why?
MySQL. While MySQL doesn't have a data type for IP addresses, it's still easy to compare IPs in MySQL. Using inet_aton you can convert your IP addresses to integers before you compare.
We can store an IP address with the help of INT unsigned. While using INSERT, include INET_ATON() and with SELECT, include INET_NTOA().
IP Network Address Data Types. IPV4 and IPV6 are abstract data types that store IPv4 and IPv6 host addresses, respectively, in binary format. IPV4 is a 4-byte host address in dotted-decimal notation (four decimal numbers, each ranging from 0 to 255, separated by dots). For example: 172.16.
In this post, we will investigate how to discover the answer to Mysql Store Ip Address using the computer language. -- `ip_address` int(4) unsigned NOT NULL INSERT INTO my_table (`ip_address`) VALUES (INET_ATON("127.0. 0.1")); SELECT INET_NTOA(ip_address) as ip FROM my_table; -- php <? php var_dump(ip2long('123.63.
I presume you're only interested in IPv4 addresses, not IPv6.
I would use an INT UNSIGNED
for the column, and then use INET_ATON
and INET_NTOA
to convert back and forth between the textual representation and the int value.
mysql> SELECT INET_ATON('192.168.10.50');
+----------------------------+
| INET_ATON('192.168.10.50') |
+----------------------------+
| 3232238130 |
+----------------------------+
1 row in set (0.00 sec)
mysql> SELECT INET_NTOA(3232238130);
+-----------------------+
| INET_NTOA(3232238130) |
+-----------------------+
| 192.168.10.50 |
+-----------------------+
1 row in set (0.00 sec)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With