DarSta
DarSta

Reputation: 155

Set value lower than 0 in PHP getting 0

I have some table in my database with column with type BIGINT: enter image description here

The problem is, when I set (by update or insert) value lower that 0 (for example -2) then in DB it is set 0. enter image description here

Do someone know why?

(I have not any procedures to change this value before insert/update). Db is MySql

Upvotes: 0

Views: 74

Answers (2)

shalvah
shalvah

Reputation: 895

It looks like what your database is actually using is a BIGINT UNSIGNED, which has a range of 0 to 18446744073709551615. And according to the MySQL Reference Manual:

When an out-of-range value is assigned to an integer column, MySQL stores the value representing the corresponding endpoint of the column data type range.

This means that attempting to store a negative number in any UNSIGNED column will wrap around to 0.

To remedy this, it's best to explicitly set the data type to be BIGINT SIGNED.

Upvotes: 1

Joris
Joris

Reputation: 2796

You can read the difference between unsigned bigint and signed bigint. MySQL Bigint

Unsigned bigint values are : 0 to 18446744073709551615
Whereas signed bigint values are : -9223372036854775808 to 9223372036854775807
So you must use the signed bigint

Upvotes: 4

Related Questions