Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing IP address in database as integer

Tags:

php

mysql

i'm storing ip adress in database as integer, with ip2long(), column type is unsigned int, but for some ip in database it saves as 0, i tested and some ip for ip2long function returns negative integer and when i insert it in database it saves as 0. i googled and everyone says that column must be unsigned int.

for example: ip2long("212.36.25.15"); will return -735831793 and in database it will be saved as 0

like image 756
Gia Nebieridze Avatar asked Sep 14 '26 12:09

Gia Nebieridze


2 Answers

Insert the ip address using INET_ATON.
eg: INET_ATON('127.0.0.1');
get back the ip address using INET_NTOA
eg: SELECT INET_NTOA('2130706433');

like image 158
Thejas Avatar answered Sep 17 '26 02:09

Thejas


You can use the MySQL function INET_ATON instead.

like image 35
Hengqi Chen Avatar answered Sep 17 '26 03:09

Hengqi Chen