简体   繁体   English

如何在PHP中使用Int64(long)而不是字符串?

[英]How to use Int64(long) in php but not string?

How to user Int64 in php like dotnet、java、c++? 如何在php,如dotnet,java,c ++中使用Int64?

For example: 例如:

The datatype is bigint(20) in mysql,I want to save value from php.I know one solution is using string in mysql procedures. 数据类型是mysql中的bigint(20),我想从php中保存值。我知道一个解决方案是在mysql程序中使用字符串。

table case: 表格案例:

CREATE TABLE `test_int64` (
  `id` bigint(20) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

mysql procedures case: mysql程序案例:

CREATE PROCEDURE `sp_test_int64`(IN p_id VARCHAR(20))
    NOT DETERMINISTIC
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
     INSERT INTO test_int64(id) VALUES(p_id+0);
END;

php code case: php代码案例:

$id = '1660820628901389';  Then execute sql 'call sp_test_int64(\''.$id.'\')'

The above method is OK, but I don't want to do it. 上面的方法没问题,但我不想这样做。 Are there any other solutions? 还有其他解决方案吗?

example 2: 例2:

I want to use PHP read MYSQL data output JSON code like this: 我想用PHP读取MYSQL数据输出JSON代码,如下所示:

{
    "id":1660820628901389
}

but not : 但不是 :

{
    "id":"1660820628901389"
}

I don't know how to do? 我不知道该怎么办?

The INT type in PHP is linked to the CPU and the version of PHP installed (you can have 32bit PHP installed on 64bit CPU). PHP中的INT类型链接到CPU 安装的PHP版本(您可以在64位CPU上安装32位PHP)。

To find out what the maximum integer PHP can support, simply 要找出PHP可以支持的最大整数,简单地说

echo PHP_INT_MAX;

And this will give you the maximum. 这将给你最大的收益。 It will give you either 2 billion, or a huge number too large to read (9.2 gazillion or so). 它会给你20亿,或者数量太大而无法读取(9.2亿左右)。

Note that it's signed (-2 billion -> +2 billion, or -9 gazillion -> +9 gazilion). 请注意它是签名的(-2亿 - > +20亿,或-9 gazillion - > +9 gazilion)。 Where as you can specify unsigned in mySQL, you don't have this option in PHP. 你可以在mySQL中指定unsigned,你在PHP中没有这个选项。

If you only have the 2 billion limit, then you have the 32 bit version of PHP installed. 如果您只有20亿个限制,那么您安装了32位版本的PHP。 You can install PHP 64 bit on a 64 bit CPU, or you'll need to handle as a string or you could use the BC Math functions to convert the string you get back from mySQL into a numeric - but I'm not sure JSON_encode will handle it*. 您可以在64位CPU上安装PHP 64位,或者您需要作为字符串处理,或者您可以使用BC Math函数将您从mySQL返回的字符串转换为数字 - 但我不确定JSON_encode会处理它*。 If you have the large number, you should be just fine and can process as an integer. 如果你有大数字,你应该很好,可以作为整数处理。

  • JSON_encode note: in the past I've noticed the JSON_encode and serialize convert large numbers to floating points, and then lose precision internally, even through they have been whole number ints. JSON_encode注意:在过去,我注意到JSON_encode和序列化将大数字转换为浮点数,然后在内部失去精度,即使它们已经是整数int。 I've not tested recently or on 64bit, but is another gotcha to watch out for. 我最近没有测试过,也没有64位测试,但另一个需要注意的是。

Edit: Here's the relevant page in the manual: http://php.net/manual/en/language.types.integer.php It also adds GMP as an alternative to BC Math 编辑:这是手册中的相关页面: http//php.net/manual/en/language.types.integer.php它还添加了GMP作为BC Math的替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM