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.

Read More

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?

Related posts

Leave a Reply

4 comments

  1. 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.

  2. 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.