Home  >  Article  >  Backend Development  >  MYSQL: Upgrading int type to bigint, impact on PHP development language

MYSQL: Upgrading int type to bigint, impact on PHP development language

不言
不言Original
2018-04-20 11:40:152693browse

The content introduced in this article is about MYSQL: upgrading the int type to bigint, and its impact on the PHP development language. It has a certain reference value. Now I share it with you. Friends in need can refer to it

because As the business grows, the original unsigned int is no longer enough and needs to be upgraded to unsigned bigint


MYSQL integer supported range:

https://dev. mysql.com/doc/refman/5.7/en/integer-types.html


Type Storage Minimum Value Maximum Value
  (Bytes) (Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
    0 255
SMALLINT 2 -32768 32767
    0 65535
MEDIUMINT 3 -8388608 8388607
    0 16777215
INT 4 -2147483648 2147483647
    0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
    0 18446744073709551615

You can see that the order of magnitude supported by bigint is very large. Although the mysql field type has been adjusted, does our development language support it?


PHP5.6 64 version:

PHP’s int type range is PHP_INT_MIN (available for PHP7) - PHP_INT_MAX,

For example, under x64-php, the maximum range of int is: 9223372036854775807, corresponding to the BIGINT type of MYSQL

! Versions before PHP7 under window are always 32-bit, and the following behavior will be different from the points listed below

! Unsigned integers are not supported

Int numbers exceeding this range will be converted to float type. We know that the data submitted by the form or retrieved from the database is of string type. When we use PHP to perform operations, the following points need to be noted:

Please refer to: http://php .net/manual/zh/language.types.integer.php

1) intval function, the output of this function only supports the int type range. If it exceeds this range, it needs to be represented by a string

$number = "9223372036854775900";
var_dump(intval($number));
//output,默认返回PHP_INT_MAX最大值
int(9223372036854775807)



2) int type as array key value , will overflow, so the solution needs to convert the key into a string

$arr = [];
$arr[9223372036854775900] = '1';
var_dump($arr);
//output
array(1) {
  [-9223372036854775808]=>
  string(1) "1"
}



##3) Impact of json_decode

If the data exceeds the range of int, the parsed data is float type

$str = '[9223372036854775807,9223372036854775907]';
var_dump(json_decode($str));
//output
array(2) {
  [0]=>
  int(9223372036854775807)
  [1]=>
  float(9.2233720368548E+18)
}


In addition, PHP Cannot support unsigned bigint



##

The above is the detailed content of MYSQL: Upgrading int type to bigint, impact on PHP development language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn