当插入值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作为参数传递
options JSON解码选项的位掩码。目前有两个
支持的选项。第一个是JSON_BIGINT_AS_STRING,它允许
将大整数强制转换为字符串,而不是默认的浮点数。
http://php.net/manual/en/function.json-decode.php
Example#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