插入像1591823907560714这样的值时,它总是以零结尾
(1591866907560700)在我的数据库中,我不知道为什么。我使用bigint作为
数据类型,这是我的sql语句:
$ user = json_decode(file_get_contents(“php:// input”));
// cast the object to an array
$user = (array)$user;
$userId = $user['id'];
$firstname = $user['firstName'];
$lastname = $user['lastName'];
$email = $user['email'];
$photoUrl = $user['photoUrl'];
$createUser = "INSERT INTO users (userId, firstname, lastname, email, photoUrl, phoneNumber)
VALUES ('".$userId."','$firstname','$lastname','$email','$photoUrl',NULL)";
我应该在插入之前解析或格式化数字吗?
解决方案发现,这个问题是由于json_decode(),JSON_BIGINT_AS_STRING应该作为参数传递
选项 JSON解码选项的位掩码。目前有两种支持的选项。第一个是JSON_BIGINT_AS_STRING,允许将大整数转换为字符串而不是浮点数,这是默认值。
http://php.net/manual/en/function.json-decode.php
示例#5大整数的json_decode()
<?php
$json = '{"number": 12345678901234567890}';
var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
?>
The above example will output:
object(stdClass)#1 (1) {
["number"]=>
float(1.2345678901235E+19)
}
object(stdClass)#1 (1) {
["number"]=>
string(20) "12345678901234567890"
}
现在该值正确插入为1591866907560714