php 错误的整数 (2147483647) 被插入到 MySQL 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10255724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:45:03  来源:igfitidea点击:

Incorrect Integer (2147483647) is inserted into MySQL?

phpmysqlinsertsteam-web-api

提问by Archey

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.

好的,所以我一直在玩弄 Steam Web API,我将其中一个值存储在一个名为$steam64. 当我使用此代码剪刀将其插入到 mysql 数据库中时,它会插入一个与存储在变量中的整数完全不同的整数。

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

$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.

var_dump($steam64);返回真正的 int,回显它也是如此。不太确定这里发生了什么,任何帮助表示赞赏。

采纳答案by Mike Purcell

It's probably because you are wrapping the query in double quotes, but the variable is in single quotes, so it's being treated as a literal, and if the column is int you will get a 0 instead.

这可能是因为您将查询用双引号括起来,但变量用单引号括起来,所以它被视为文字,如果列是 int,您将得到一个 0。

Try this:

尝试这个:

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

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

另外,在我受到抨击之前,请务必阅读SQL 注入,以防您不清理直接发布到 sql 语句中的变量。

-- Update --

- 更新 -

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. Greatarticle about max ints here, and here.

基于您对“价值被倾销”的评论;您尝试插入的数字对于 32 位系统来说太大了。32 位的最大值为4,294,967,295,64 位的最大值为18,446,744,073,709,551,615。我建议将您的列转换为varchar(100)散列而不是 int,或切换到 64 位系统。关于 max ints 的文章在这里这里

回答by George

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

2147483647 是 mysql 的最大 int 值。只需将类型从 int 更改为 bigint。

回答by Lyserty

While I was playing with SQL and MySQL had the same problem MySQL int data type. Modifying data type from intto bigintfixed issue.

当我在玩 SQL 时,MySQL 有同样的问题 MySQL int 数据类型。将数据类型从int修改为bigint 已解决问题。

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

MySQL 整数类型 http://dev.mysql.com/doc/refman/5.7/en/integer-types.html

ALTER TABLE tablename MODIFY columnname BIGINT; 

回答by Ron Varghese

Simply Change the data type from INT to BIGINT

只需将数据类型从 INT 更改为 BIGINT

回答by Nadeem Shakya

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

数据类型 INT 的最大值是 2147483647。如果您插入的数字大于 2147483647,则会导致问题。对于解决方案,将数据类型从 INT 更改为 BIGINT,因为 BIGINT 的最大值为 9223372036854775807,它可能会解决您的问题。看看这个网站:https: //dev.mysql.com/doc/refman/5.7/en/integer-types.html

回答by Rodney Salcedo

The integer type INTis 4Bytes storage, you get from -2^(4*8-1)=-2147483648to 2^(4*8-1)-1=2147483647, when you have "signed" flags, if you change the flags to unsignedyou will have a range from 0to 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

整数类型INT是 4Bytes 存储,您可以从-2^(4*8-1)=-2147483648to获得2^(4*8-1)-1=2147483647,当您有“有符号”标志时,如果将标志更改为无符号,您的范围将从02^(4*8)-1。MySQL 支持 BIGINT 为 8Bytes 存储。如果您尝试保存更大的值,您将保存范围的最大值

回答by Peter Tountas

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':

同意将数据类型从 INTEGER 更改为 BIGINT。目前正在使用 node.js/sequelize 构建一个 web 应用程序,以下重构解决了从 react-redux 表单操作为“2147483647”的电话号码发布:

clientPhone: {
    type: DataTypes.BIGINT,
    allowNull: true
},

回答by lalit mohan

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

自动增量的大问题是它错误地从最后一个条目开始,如果它的值非常大,那么在插入值中开始问题......使用我们预定义的数据类型
在此处输入图片说明

回答by MatiiTv

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

最简单的方法是将 MySQL 的“int”更改为“varchar”。

回答by Firmansyah

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;