Unsigned int to signed in php

后端 未结 4 1112
别跟我提以往
别跟我提以往 2021-01-06 00:43

It appears, that in 32bit OS ip2long returns signed int, and in 64bit OS unsigned int is returned.

My application is working on 10 servers, and some are

4条回答
  •  清酒与你
    2021-01-06 00:58

    Fwiw, if you're using MySQL it's usually a lot easier and cleaner if you just pass in the IPs as strings to the database, and let MySQL do the conversion using INET_ATON() (when INSERTing/UPDAT(E)'ing) and INET_NTOA() (when SELECTing). MySQL does not have any of the problems described here.

    Examples:

    SELECT INET_NTOA(ip_column) FROM t;
    
    INSERT INTO t (ip_column) VALUES (INET_ATON('10.0.0.1'));
    

    The queries are also much more readable.

    Note that you can not mix INET_NTOA()/INET_ATON() in MySQL with ip2long()/long2ip() in PHP, since MySQL uses an INT UNSIGNED datatype, while PHP uses a signed integer. Mixing signed and unsigned integers will seriously mess up your data!

提交回复
热议问题