Question

How to user Int64 in php like dotnet、java、c++?

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.

table case:

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

mysql procedures case:

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:

$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:

I want to use PHP read MYSQL data output JSON code like this:

{
    "id":1660820628901389
}

but not :

{
    "id":"1660820628901389"
}

I don't know how to do?

Était-ce utile?

La solution

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).

To find out what the maximum integer PHP can support, simply

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).

Note that it's signed (-2 billion -> +2 billion, or -9 gazillion -> +9 gazilion). Where as you can specify unsigned in mySQL, you don't have this option in PHP.

If you only have the 2 billion limit, then you have the 32 bit version of PHP installed. 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*. 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. I've not tested recently or on 64bit, but is another gotcha to watch out for.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top