2

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?

0

4 Answers 4

7

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.

3
  • Why would you consider VARCHAR as being more appropriate here?
    – thejh
    Nov 2, 2010 at 20:38
  • @thejh the user ID looks like a random value chosen for security purposes rather than numbering (they hardly have trillions of users). Should the need arise to create even longer IDs in the future, even BIGINT will break at some point, a problem you don't get with a long VARCHAR
    – Pekka
    Nov 2, 2010 at 20:40
  • 1
    An anonymous user tried to add this as an edit, I figured it's worth mentioning: "While this is true, I had the same problem using $wpdb->insert with a %d in the $wpdb->prepare statement. My database was using BIGINT, but Wordpress when cleansing the input converted it to a regular int and would keep inserting the incorrect value. I changed it to a %s (leaving the column as a BIGINT) and it now stores the correct value".
    – Pekka
    Mar 30, 2014 at 19:53
1

int will not hold that large of a value. You can try bigint. See the MySQL documentation

0

Look here: http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html

The maximum for int is 2147483647, try using bigint.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.