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