bigint在mysql中创建为int

rkkpypqq  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(400)

我在wordpress中有一个mysql数据库表,在那里我声明了一个bigint字段,没有任何问题。
但是当我在安装在自己计算机上的mysql中创建相同的表时,大的数字被存储为2147483647,这是int的最大值。
你知道为什么会这样吗?
这是table,

CREATE TABLE inPxUtBI_follow_data_tokens (
  id int(20) unsigned NOT NULL,
  screen_name varchar(15) NOT NULL,
  token tinytext,
  secret tinytext,
  time_data_cursor datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  friends_cursor bigint(20) NOT NULL DEFAULT -1,
  followers_cursor bigint(20) NOT NULL DEFAULT -1,  
  datetime_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  time_data_cursor_index smallint(10) unsigned NOT NULL,
  PRIMARY KEY  (screen_name)
) ;

编辑:版本信息

mysql> \s
--------------
C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe  Ver 14.14 Distrib 5.7.22,
 for Win64 (x86_64)

Connection id:          2
Current database:
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         5.7.22-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    cp850
Conn.  characterset:    cp850
TCP port:               3306
Uptime:                 30 min 3 sec

Threads: 1  Questions: 7  Slow queries: 0  Opens: 109  Flush tables: 1  Open tab
les: 102  Queries per second avg: 0.003
--------------

编辑:数据信息
这是更新sql,

update inPxUtBI_follow_data_tokens set followers_cursor = %d WHERE screen_name = '%s' ["1599757792260458963","xxx"]
update inPxUtBI_follow_data_tokens set friends_cursor = %d WHERE screen_name = '%s' ["1600189794255483463","xxx"]

这是最新的一行,

id,screen_name,token,secret,time_data_cursor,friends_cursor,followers_cursor,datetime_created,time_data_cursor_index
111,xxx,yyy,zzz,"2018-05-13 15:37:06",2147483647,2147483647,"2018-05-13 11:59:57",9

编辑:php代码

public static function setUserCursor($table, $field, $screen_name, $next_cursor) {
        flog(DEBUG, 'setUserCursor', $next_cursor);
        $update_count = 0;
        if ($next_cursor > 0) {
            global $wpdb;
            $sql = "update $table set $field = %d WHERE screen_name = '%s'";
            $sqldata = array($next_cursor, $screen_name);
            flog(DEBUG, 'setUserCursor', $sql . ' ' . json_encode($sqldata));

            $update_count = $wpdb->query($wpdb->prepare($sql, $sqldata));
        }
        return $update_count;
    }

编辑:@progman的建议引起了一些好奇
在我的本地机器上,

update inPxUtBI_follow_data_tokens set friends_cursor = %d WHERE screen_name = '%s' ["1557868487712412145","xxx"]
update inPxUtBI_follow_data_tokens set friends_cursor = 2147483647 WHERE screen_name = 'xxx'

但在远程服务器上,

update inPxUtBI_follow_data_tokens set friends_cursor = %d WHERE screen_name = '%s' [1600189862942848368,"xxx"]
update inPxUtBI_follow_data_tokens set friends_cursor = 1600189862942848368 WHERE screen_name = 'xxx'

请注意第一行中“1557868487712412145”值周围的引号。
我已经把事情固定在这个函数中,在这个函数中检索json数据。

function getFollowersIDs($user, $count, $cursor) {
    $url = $this->api . '1.1/followers/ids.json';
    $getfield = '?screen_name=' . $user . '&skip_status=1&count=' . $count . '&cursor=' . $cursor;
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchange($this->settings);
    $data = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
    $rtn = json_decode($data, true, 512, JSON_BIGINT_AS_STRING);
    flog(VERBOSE, 'getFollowersIDs', $data);
    flog(DEBUG, 'getFollowersIDs', 'CURSOR: ' . json_encode(array($rtn['next_cursor'])));
    flog(DEBUG, 'getFollowersIDs', is_string($rtn['next_cursor']) ? $rtn['next_cursor'] . ' IS string' : $rtn['next_cursor'] . ' IS NOT string');

    return $rtn;
}

这些日志分别用于本地和远程,

[getFollowersIDs] {"ids":[1492183206,913536285461147649,825717050538618880,961964711720910848,591132453,248777189,232207153,400934967,77967828,443634207],"next_cursor":1600147168522111920,"next_cursor_str":"1600147168522111920","previous_cursor":0,"previous_cursor_str":"0"}
[getFollowersIDs] CURSOR: ["1600147168522111920"]
[getFollowersIDs] 1600147168522111920 IS string

[getFollowersIDs] {"ids":[59150726,901375444934635520,385097832,331067377,194220828,540223123,2746743156,2271848935,819196471845253121,963324881906511877],"next_cursor":1597756074201108094,"next_cursor_str":"1597756074201108094","previous_cursor":-1597922052519508811,"previous_cursor_str":"-1597922052519508811"}
[getFollowersIDs] CURSOR: [1597756074201108094]
[getFollowersIDs] 1597756074201108094 IS NOT string

那么,为什么json\u decode在一台机器上返回字符串,在另一台机器上返回bigint呢?

sshcrbum

sshcrbum1#

经过大量的调查,我发现我的本地php是32位的。
安装64位php解决了这个问题。
所以,有了这些数据,

[{"ids":[59150726,901375444934635520,385097832,331067377,194220828,540223123,2746743156,2271848935,819196471845253121,963324881906511877],"next_cursor":1597756074201108094,"next_cursor_str":"1597756074201108094","previous_cursor":-1597922052519508811,"previous_cursor_str":"-1597922052519508811"}]

这个代码,

echo 'CURSOR: ' . json_encode(array($rtn[0]['next_cursor']))."\n";

生成此结果(值周围没有引号), CURSOR: [1597756074201108094] .
我认为不同版本之间的差异相当模糊和误导!
感谢@progman带领我做出决定。

sycxhyv7

sycxhyv72#

似乎您的本地计算机使用4字节整数(32或64位php),而远程计算机使用8字节整数(64位php)。整数的大小定义了bigint的大小:

$a = json_decode('{"n":1600147168522111920}', true, 512, JSON_BIGINT_AS_STRING);
var_dump(PHP_INT_SIZE, PHP_INT_MAX, $a);

// local machine output
int(4)
int(2147483647)
array(1) {
  ["n"]=>
  string(19) "1600147168522111920"
}

// remote machine output
int(8)
int(9223372036854775807)
array(1) {
  ["n"]=>
  int(1600147168522111920)
}

如你所见,价值 1600147168522111920 不能放入4字节整数,因此转换为字符串。
现在,我不知道 wpdb::prepare ,但显然它会试图转换 %d 到特定于平台的整数,截断 16001471685221119202147483647 :

echo sprintf("%d", "1600147168522111920");
// 2147483647

解决办法是改变 %d%s . 但在此之前,请确保所讨论的值看起来像一个有效的大整数。

相关问题