views:

251

answers:

2

I came accross this statement for inserting an IP into a mysql table.

INSERT INTO `best` VALUES (132+256*(172+256*(109+256*(115))

I would like to know why an IP is being inserted this way, and how to actually work out what IP is being inserted

+1  A: 

Its prefer storing the IP address in decimal (integer) as it makes it easier to work with later.

But you can use MySQL commands to easily convert the data. For instance if you retrieved the IP you can use the inet_aton('$ip') command to convert the insert the IP Address ($ip) into decimal and then add the data into the database. You can convert the Decimal Address back to octets by using inet_ntoa

see this links:

http://www.countryipblocks.net/networking/ip-address-conversion-to-decimal-and-binary-equivalents/

http://www.webproworld.com/web-programming-discussion-forum/85464-storing-ip-location-mysql-database.html

Haim Evgi
Interesting. So is there no way to look at the string above and work out the IP?
Anonymous
•INET_NTOA(expr) Given a numeric network address in network byte order (4 or 8 byte), returns the dotted-quad representation of the address as a string. mysql> SELECT INET_NTOA(3520061480); -> '209.207.224.40'
Haim Evgi
The IP address from your example above is 115.109.172.132. To match the numbers is up to you ;-)
Frank Bollack
http://ipinfodb.com/ use this technique to store ip address in their ip to country. For the column type, they use `bigint(20)` in the latest database release.
Donny Kurnia
A: 

that's way used to reduce the space used to store ip.

with this way you will store IPs in int type field, that is smaller in space than char type

assaqqaf