mjsiemer
mjsiemer

Reputation: 89

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?

Upvotes: 2

Views: 242

Answers (4)

Stephen P
Stephen P

Reputation: 14800

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.

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72971

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

Upvotes: 1

thejh
thejh

Reputation: 45568

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

The maximum for int is 2147483647, try using bigint.

Upvotes: 0

Pekka
Pekka

Reputation: 449395

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.

Upvotes: 7

Related Questions