Value inserted into MYSQL table differs from original value
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 - a VARCHAR
.
The (15)
after the INT
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 try bigint
. See the MySQL documentation
Look here: http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html
The maximum for int
is 2147483647, try using bigint
.
From the MySql Manual
INT[(M)] [UNSIGNED] [ZEROFILL]
A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
The (15) on your INT is the suggested display width, and doesn't make an INT hold bigger numbers. You need a BIGINT.
精彩评论