Incorrect Integer (2147483647) is inserted into MySQL?
Asked Answered
S

11

50

Alright, so I've been toying around with the Steam Web API, I have one of the values stored in a variable called $steam64. When I use this code snipper to INSERT it into a mysql database it insert a completley different integer than what is stored in the variable.

$sql_query = "INSERT INTO users_info (steam64) VALUES ('$steam64')";

var_dump($steam64); returns the real int, so does echoing it. Not too sure what is going on here, any help is appreciated.

Selfsustaining answered 21/4, 2012 at 2:42 Comment(5)
What is inserted (and what was dumped) and what is the column type? It's likely not a "bug" in MySQL, which means the problem lies elsewhere...Morphosis
shouldn't need the single quotes on an integer. it may be trying to insert the string '$steam64' as an integer, converting it in the process.Sidereal
@pst 2147483647 is being inserted, 76561197989628470 is being dumped, column type is int(255).Selfsustaining
@Sidereal just having it VALUES ($steam64) still inserts the wrong int.Selfsustaining
dev.mysql.com/doc/refman/5.5/en/integer-types.htmlBollen
B
41

Based on your comment of "value being dumped"; the number you are trying to insert is too large for 32-bit systems. The max for 32-bit is 4,294,967,295, and the max for 64-bit is 18,446,744,073,709,551,615. I'd recommend converting your column into a varchar(100) hash rather than an int, or switch to a 64 bit system. Great article about max ints here, and here.


Also, before I get flamed, be sure to read up on SQL injection in case you are not sanitizing variables being posted directly into sql statements.

Braga answered 21/4, 2012 at 2:52 Comment(6)
Posted update. The number you are trying to insert is too big.Braga
Thanks :) Is it PHP or MySQL that has the limit?Selfsustaining
It's a system limit, so any app trying to access a number that big will result unexpected behavior.Braga
Alright quick question with that, if i wanted to have a for loop with a variable that is something like 76561197980000000, would that even be possible?Selfsustaining
Yes, as long as it's not an int.Braga
@Pst: Thanks for the correction, I should have known when pasting the values in.Braga
A
71

2147483647 is the largest int value for mysql. Just change the type from int to bigint.

Audit answered 22/7, 2013 at 8:49 Comment(1)
It's worth noting that BIGINT has a limit of -9223372036854775808 to 9223372036854775807 (signed) or 0 to 18446744073709551615 (unsigned). if you want the larget POSITIVE ONLY integer then use something like COLUMNNAME BIGINT(32) UNSIGNED DEFAULT 0Headgear
B
41

Based on your comment of "value being dumped"; the number you are trying to insert is too large for 32-bit systems. The max for 32-bit is 4,294,967,295, and the max for 64-bit is 18,446,744,073,709,551,615. I'd recommend converting your column into a varchar(100) hash rather than an int, or switch to a 64 bit system. Great article about max ints here, and here.


Also, before I get flamed, be sure to read up on SQL injection in case you are not sanitizing variables being posted directly into sql statements.

Braga answered 21/4, 2012 at 2:52 Comment(6)
Posted update. The number you are trying to insert is too big.Braga
Thanks :) Is it PHP or MySQL that has the limit?Selfsustaining
It's a system limit, so any app trying to access a number that big will result unexpected behavior.Braga
Alright quick question with that, if i wanted to have a for loop with a variable that is something like 76561197980000000, would that even be possible?Selfsustaining
Yes, as long as it's not an int.Braga
@Pst: Thanks for the correction, I should have known when pasting the values in.Braga
D
11

While I was playing with SQL and MySQL had the same problem MySQL int data type. Modifying data type from int to bigint fixed issue.

MySQL Integer Types http://dev.mysql.com/doc/refman/5.7/en/integer-types.html

ALTER TABLE tablename MODIFY columnname BIGINT; 
Diastole answered 3/8, 2016 at 19:10 Comment(0)
G
9

Simply Change the data type from INT to BIGINT

Grazia answered 29/3, 2018 at 4:54 Comment(0)
A
7

The largest value for data type INT is 2147483647. If the number you're inserting is bigger than 2147483647, then it will cause the problem. For solution, change the data type from INT to BIGINT as BIGINT has a maximum value of 9223372036854775807, it might solve your problem. Have a look at this site: https://dev.mysql.com/doc/refman/5.7/en/integer-types.html

Amadou answered 30/6, 2017 at 2:13 Comment(0)
T
5

The integer type INT is 4Bytes storage, you get from -2^(4*8-1)=-2147483648 to 2^(4*8-1)-1=2147483647, when you have "signed" flags, if you change the flags to unsigned you will have a range from 0 to 2^(4*8)-1 . MySQL support BIGINT being 8Bytes storage. If you try save a value greater, you will save the max value of the range

Translunar answered 25/8, 2015 at 18:57 Comment(0)
T
1

Go to operations-> table options -> change increment values to minimum or whatever you want to increment..

the big problem of autoincrement is it's start from last entry by mistake if its very large value then start problem in insert value.. with our predefined datatype
enter image description here

Tertia answered 4/8, 2015 at 13:10 Comment(0)
G
1

Agree with the datatype change to BIGINT from INTEGER. Currently building a web app with node.js/sequelize the below refactor solved the phone number post from react-redux form manipulated to '2147483647':

clientPhone: {
    type: DataTypes.BIGINT,
    allowNull: true
},
Gasholder answered 6/6, 2017 at 23:54 Comment(0)
S
0

I had the same problem but not with Varchar. The problem I had was that I was performing an INSERT INTO with a bad order of columns. For anyone who see this maybe is the order of cols that u are using in a subquery

Stanleystanly answered 12/7, 2022 at 12:23 Comment(0)
B
-5
CREATE TABLE `dvouchers` (
  `2147483647` int(3) NOT NULL auto_increment,
  `code` varchar(12) NOT NULL default '1',
  `type` char(1) NOT NULL default '$',
  `count` int(3) unsigned NOT NULL default '0',
  `amount` int(3) unsigned default '0',
  `notes` text,
  `expiryDate` date default NULL,
  `fkUserAdminId` int(11) NOT NULL default '0',
  PRIMARY KEY  (`2147483647`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Bryce answered 10/8, 2013 at 6:55 Comment(2)
hmm ... and this answers the question because ... ?Aronson
This makes no sense, do not try to infer any help from this answer if you're here with this issue.Headgear
T
-5

Easiest way is change in MySQL "int" to "varchar".

Twit answered 19/2, 2016 at 16:58 Comment(1)
Do not do this. Never do this. VARCHAR is a string representation and not a numeric one. It would not be possible to run mathematical operations on a string.Erelia

© 2022 - 2024 — McMap. All rights reserved.