我在 WordPress 中有一个 mysql 数据库表,我在其中声明了 BIGINT 字段,没有任何问题。
但是当我在安装在我自己的计算机上的 mysql 中创建相同的表时,大数字存储为 2147483647,这是 INT 最大大小。
有什么想法为什么会发生这种情况吗?
这是表格,
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_decode 在一台机器上返回字符串,而在另一台机器上返回 bigint?
最佳答案
似乎您的本地计算机使用 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
转换为平台特定的整数,截断 1600147168522111920
到2147483647
:
echo sprintf("%d", "1600147168522111920");
// 2147483647
解决方案是将%d
更改为%s
。但在执行此操作之前,请确保相关值看起来像一个有效的大整数。
关于php - BIGINT 在 mysql 中创建为 INT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50309697/