【问题标题】:When I try to insert ip2long Integer, the wrong Integer gets inserted into table当我尝试插入 ip2long Integer 时,错误的 Integer 被插入到表中
【发布时间】:2015-03-09 01:37:41
【问题描述】:

我的代码:

$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"));
}

但是当我检查我的 MySQL 表时,ip 列下新插入的值是 2147483647,即“127.255.255.255”。

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

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

【问题讨论】:

    标签: php mysql ip


    【解决方案1】:

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

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

    2953470927 位超出有符号INT 的范围。

    参考:MySQL: Integer types

    【讨论】:

      【解决方案2】:

      不幸的是,取决于您的系统(32 位与 64 位)PHP 返回不同的值。

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

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

      【讨论】:

        猜你喜欢
        • 2015-06-06
        • 1970-01-01
        • 2015-04-30
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 2018-04-21
        • 1970-01-01
        相关资源
        最近更新 更多