如何将32位系统上的ip2long负值存储到MySQL

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?

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.