问题
I am using mysql db to save id's. It was working fine.
But now an id like this 10000000754987. Is stored as 1.0000000754987E+14. How I can I fix it.
The field is set to varchar 255 characters limit.
I appreciate any help.
Thanks.
回答1:
You're apparently using PHP to generate that ID.
Since you didn't mention what's exactly happening, I can only assume certain reasons for your database design.
First off, you could store that number as bigint
and not varchar
. It's an integer you're saving, I see no reason why you'd use varchar and inherently waste more space than needed.
bigint
uses 8 bytes to store a number. That means that every number with more than 8 digits stored in varchar field would use more space than a bigint
field that can store numbers up to 2^64.
Second, make sure you don't use any number formatting before sending the result of your calculation operation to the db. I copy/pasted the integer you posted (10000000754987) and php doesn't automatically convert it to scientific notation so my guess is that you have something else going on there in the background with that number generation - turn it off if possible and store the number in appropriate field type (bigint). It'd also be useful if you posted additional info about your app and what it does, because error isn't always where people thing it is.
回答2:
Your problem is that the numerical IDs have gotten too big for PHP/MySQL to handle.
Change the data type to VARCHAR(36)
and CakePHP will now start using UUIDs instead of numerical IDs, you will have to do this on any foreign keys for this table too. This will prevent this problem from happening in the future.
Additionally, as it's varchar, it should work with your current IDs too..
来源:https://stackoverflow.com/questions/6664640/problem-with-storing-large-numbers-in-mysql-db