3

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

tadman
  • 208,517
  • 23
  • 234
  • 262
Gia Nebieridze
  • 141
  • 3
  • 14
  • 2
    You'll need to change the type of your database column from 'integer' to 'unsigned int' – aashnisshah Mar 22 '16 at 00:10
  • column is unsigned int, but for some ips it saves as 0. – Gia Nebieridze Mar 22 '16 at 00:14
  • http://stackoverflow.com/questions/11210713/ip-addresses-get-stored-as-0-when-converting-with-php-ip2long-function This may be of use to you – JackGroves Mar 22 '16 at 00:17
  • 2
    Why are you converting them like this? A`VARCHAR` column is a better plan since it can store IPv4 and IPv6 addresses. Your numerical solution is IPv4 only, setting yourself up for failure later. An IPv4 address is an unsigned 32-bit value, but unless you're storing billions of them, use strings. It's safer and more flexible. – tadman Mar 22 '16 at 00:56
  • Postgres has special Network Address Types: cidr and inet since 9.1. They are great. – alfthan Mar 22 '16 at 01:11

2 Answers2

4

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');

Thejas
  • 353
  • 6
  • 19
3

You can use the MySQL function INET_ATON instead.