0

I have a table in mysql with a field called "userId":

`userId` int(20) NOT NULL

Insert statement is:

$result = mysql_query("INSERT INTO users (userId, firstName, lastName, dateCreated) VALUES (".$me['userId'].", '".$me['first_name']."', '".$me['last_name']."', CURDATE()) ")or die(mysql_error());

The value of userId when echoed in php is 100000517980247, but when inserted it changes to 2147483647. When I insert it into a varchar field it's fine. This has to be something really simple but I searched around a bit and I'm just not seeing it.

2 Answers 2

10

MySQL ints are a signed 32bit integer. You're inserting a number that's far above the limit, so you're seeing the maximum possible integer, which is 2147483647.

Change the data type to bigint for a 64bit data type, which will handle your number.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! Seems like an exception would be more useful in this case, rather than just inserting an acceptable value.
You can turn on strict mode (dev.mysql.com/doc/refman/5.0/en/…), which'd make MySQL reject any out-of-bounds values. Otherwise it'll silently coerce the value to fit into the field
2

You use the type INT. And you need to use type BIGINT.

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.