Incorrect Integer (2147483647) is inserted into My

2019-01-01 13:34发布

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.

10条回答
千与千寻千般痛.
2楼-- · 2019-01-01 14:02

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.

Try this:

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

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.

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

查看更多
余欢
3楼-- · 2019-01-01 14:04

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

查看更多
伤终究还是伤i
4楼-- · 2019-01-01 14:08

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; 
查看更多
余生无你
5楼-- · 2019-01-01 14:08

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
},
查看更多
倾城一夜雪
6楼-- · 2019-01-01 14:10

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

查看更多
大哥的爱人
7楼-- · 2019-01-01 14:12

Simply Change the data type from INT to BIGINT

查看更多
登录 后发表回答