I have a column of type integer
with length 10:
`some_number` int(10) unsigned NOT NULL
Into this column I insert a number that is too long:
$some_number = 715988985123857;
$query = "INSERT INTO this_table SET some_number = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('i', $some_number);
$stmt->execute();
When I look at what is in the table, the number is now:
2147483647
How and why did 715988985123857
turn into 2147483647
?
Why didn't it get truncated?
What is the mechanism behind this transformation, and can the resulting number be calculated with some formula?
I'm not looking for a solution. I just want to understand the specific number.
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
The integer overflow will set the max allowed number in the DB as
So you need
bigint
datatype for storing bigger integerWell as far as i know, it doesn't have anything to do with PHP, bcx.
So it will be the functionality of SQL which changes the value.
The behavior depends on MySQL Strict SQL Mode setting:
There is a chapter in MySQL manual explaining how MySQL handles out-of-range values: Out-of-Range and Overflow Handling:
In case of Integer Type, the maximum value is:
Since your query did not throw error, but instead, the column got updated with max allowed value, you can assume that Strict SQL Mode is not enabled on your server. You can verify that by running:
None of them will contain
STRICT_TRANS_TABLES
norSTRICT_ALL_TABLES
values (more about the values in MySQL man:sql_mode
).Take this example to test the different behaviors between modes:
In short, with strict mode out-of-range value produces an error, without - the value gets adjusted to the allowed limit and a warning is produced.
As for this question:
I'm not aware of any simple and pure MySQL formula. You would have to check the data type and length:
...and based on that determine allowed limit.
Non-sql? Data validation (server- and client-side) is the solution.