4

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?

slimboy
  • 55
  • 1
  • 2
  • 8
  • Is your server CPU architecture of 64bits? are your PHP binaries compiled for 64bits? – Mahn Aug 21 '12 at 03:53
  • There shouldn't be a need to do type mangling within MySQL, it should implicitly cast numeric strings to int64. – Ja͢ck Aug 21 '12 at 05:03
  • @Mahn I think CPU is 32 or 64 bits does not matter! Just data type. Moreover, my CPU is 64bits. – slimboy Aug 21 '12 at 06:36

1 Answers1

6

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

Robbie
  • 17,605
  • 4
  • 35
  • 72
  • Thanks very much. My PHP_INT_MAX is 9223372036854775807. – slimboy Aug 21 '12 at 07:22
  • Since you seem to be running the 64bits version, to solve your json "problem" simply typecast the variable to integer before using it with json (eg `(int) $gazillionID`) – Mahn Aug 21 '12 at 14:04
  • @Mahn Yea, your are right. At first I thought to be wrong. Thanks! – slimboy Aug 21 '12 at 16:47
  • In dotnet,CPU is 32 or 64 bits does not matter. You can use int32 or int64(eg int.Parse("1234") Int64.Parse("1660820628901389") ), but PHP is different. At first I thought that they are the same,but is not. – slimboy Aug 21 '12 at 17:04
  • @slimboy. Sorry for delayed response. As Mahn said (thanks) you should be able to simply cast as int in your array and convert to JSON. As I waned, if you get X.00001 or X.99997 etc then internally it's converted to a float and you need to do it manually (it can't handle floats very well) - but I've not seen for a long time and I doubt that's still an issue. – Robbie Aug 22 '12 at 01:30
  • @Robbie Never mind! About int value range, I think 9223372036854775807 is enough to meet my needs. Now I do it as Mahn said(eg: (int)$data["id"] ). – slimboy Aug 22 '12 at 03:40