当我尝试插入 ip2long 整数时,错误的整数被插入 table

When I try to insert ip2long Integer, the wrong Integer gets inserted into table

我的代码:

$ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'][0]); // Gives an IP address string. Example: "176.10.99.207"

$ipL = ip2long($ip); // An integer, which for this example would be 2953470927.

if( $stmt_insert_ip = $ip_link->prepare("INSERT INTO dataTable(ip,datetime) VALUES(?,?)") ){
  $stmt_insert_ip->bind_param('is',$ipL,date("Y-m-d H:i:s"));
}

但是当我查看我的MySQLtable时,ip栏下新插入的值是2147483647,即“127.255.255.255”。

当我 运行 echo($ipL); 时,我得到 2953470927。

H-E-Double 曲棍球棒是怎么回事?

我假设您在数据库中使用了 INT 作为数据类型。简而言之,只需将 ip 列的数据类型更改为 BIGINT 即可解决问题。

INT range (-2147483648, 2147483647)
BIGINT range (-9223372036854775808, 9223372036854775807)

2953470927 超出了有符号 INT.

的范围

参考:MySQL: Integer types

很遗憾,取决于您的系统(32 位与 64 位)PHP returns 不同的值。

在 32 位系统上它使用有符号整数,而在 64 位机器上它使用无符号整数,因此您需要相应地存储数据。

根据您返回的内容,您似乎使用的是 64 位机器,因此请将您的 mysql 列更改为无符号整数,或者有符号或无符号 bigint