I’m having the strangest problem with a value changing when I insert it to a MYSQL table.
This is for a WordPress theme.
Here’s the gist of the code:
$user_id = $_COOKIE['user-id'];
$wpdb->insert($custom_table,array('user_id'=>$user_id));
The type of the ‘user_id’ field of $custom_table is int(15).
I’m currently testing on myself. The user id stored in my cookie is 4022466561291. It’s being stored in the database as 2147483647.
I’ve also tried various combinations of this setup:
$user_id = intval($_COOKIE['user-id']);
$wpdb->insert($custom_table,array('user_id'=>$user_id),array('%d'));
with the same results.
Any ideas?
That is because 2147483647 is the maximum value of a signed 4-byte
INT
.You need to use a 8-byte
BIGINT
or – maybe more appropriate here – aVARCHAR
.The
(15)
after theINT
is a bit misleading. It is a optional display length limitation only. It doesn’t change the actual capacity of the data type.int
will not hold that large of a value. You can trybigint
. See the MySQL documentationLook here: http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html
The maximum for
int
is 2147483647, try usingbigint
.From the MySql Manual
The (15) on your INT is the suggested display width, and doesn’t make an INT hold bigger numbers. You need a BIGINT.