我在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呢?
答案 0 :(得分:1)
好像你的本地机器使用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
。但在此之前,请确保有问题的值看起来像一个有效的大整数。
答案 1 :(得分:1)
经过多次调查后,我发现我的本地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带领我解决问题。