BIGINT in MySQL Changes a my number? Why does it do this?

bigint, biginteger, mysql, php

Solution

Numbers lose their precision from PHP_INT_MAX onwards. See also: http://www.php.net/manual/en/reserved.constants.php#constant.php-int-max

After that they are turned into floats which have limited precision and causes weird rounding issues. The only way to support BIGINT in PHP is by using strings.

Problem

I'll try and keep this simple. I'm using a BIGINT data type on a MySQL database table. I have a function generating a unique random number that may span anywhere from 12 digits to 13 digest long. When I insert into the database with a digit that is 12 digits in length, it enters it just fine, but when I use a value that is 13 digits or longer, it seems like it rounds up or something. here is the php ``` $defaultText = 'some string'; //just some string $web_id = 12; $element_id = 23112182735363; //case 1 (doesn't work) //$element_id = 2311218333205; //case 2, does work () mysql_query("INSERT INTO tableName (web_id, element_id, content) VALUES ($web_id, $element_id, '".mysql_real_escape_string($defaultText)."')"); ``` results: in case one, it inserts a slightly different number, usually rounds up for some reason. in case two, it inserts the number just fine! Maybe someone can help shed some light on this mystery! Thanks again! the big int datatype: ``` bigint(200) ```

Original source