0

I have to store an IP address into a MySQL table running on a 32 bit system.

On a 64 bit system I would simply use INT(10) UNSIGNED since ip2long will always be an absolute value.

How to make it work on 32 bit? I have two options.

  1. Remove UNSIGNED and store negative values, but I'm not sure if there is still a chance of values being too large or too small once this is done (because the upper limit will be decreased by half to allow for the lower limit to be negative)
  2. Use sprintf("%u",ip2long( $ip )) checkout this post (will be probably slower than 1.)

Any other solutions?

Community
  • 1
  • 1
Aley
  • 8,540
  • 7
  • 43
  • 56

1 Answers1

1

You can try to convert IP not in PHP but in SQL insert query using INET_ATON function https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton. You need to use INT UNSIGNED as your column type.

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
  • I would certainly *strongly* recommend using the underlying database to convert string representations to numbers - it simplifies a lot of the query processing. You can use BIGINT instead of UNSIGNED INT, or even consider using BINARY(16) to accomodate IPV6 – symcbean Oct 09 '15 at 13:09
  • `INET_ATON` is definitely the recommended way if you write plain SQL, but what if you have to use a database wrapper e.g. WordPress database class to insert an IP? You write plain SQL or use PHP instead. – Aley Oct 12 '15 at 10:31