PHP-MySQL-如何安全地增加 MySQL 整数字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2033537/
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-25 04:44:26  来源:igfitidea点击:

PHP-MySQL-How to safely increment MySQL integer field?

phpmysqltransactionscounter

提问by John

I want to increment a field value safely using php and mysql.

我想使用 php 和 mysql 安全地增加字段值。

  1. What type of table/field must I use?

  2. Is there a minimum version of MySQL I must use?

  3. What's the sql code for this, safe transaction for MySQL?

  1. 我必须使用什么类型的表/字段?

  2. 我必须使用 MySQL 的最低版本吗?

  3. MySQL 的安全事务的 sql 代码是什么?

回答by Chris Tonkinson

By what type of "table" I assume you mean storage engine. Anything that supports mutations (i.e. not "archive" or "black hole")

我假设您指的是存储引擎的“表”类型。任何支持突变的东西(即不是“存档”或“黑洞”)

Any numeric field will do (tinyint, int, float, etc). That said, there's no special PHP code, just the SQL for incrementing the desired field:

任何数字字段都可以(tinyint、int、float 等)。也就是说,没有特殊的 PHP 代码,只有用于增加所需字段的 SQL:

UPDATE table SET field = field + 1 WHERE [...]

If you want a transaction, then pack the above query into a transaction. As for MySQL version, I agree with @hsz - use the most current version possible.

如果你想要一个事务,那么把上面的查询打包成一个事务。至于 MySQL 版本,我同意 @hsz - 尽可能使用最新版本。

回答by hsz

If you are talking about primary key then set idcolumn as primaryand auto_increment.

如果您正在谈论主键,则将id列设置为primaryand auto_increment

Increasing field is looking like that:

增加字段看起来像这样:

UPDATE table SET field = field + 1 WHERE id = 9

About MySQL version - use the newest you can. ;)
> 5.0will be fine.

关于 MySQL 版本 - 使用最新的。;)
> 5.0会好的。

回答by Devner

1.what type of table/field must I use?

1.我必须使用什么类型的表/字段?

--> The type of table depends on what you have planned for your application. It could be Innodb or Myisam. I suggest you to use numeric column so that you can increment/decrement them. Do NOT make it UNSIGNED if you plan to allow negative numbers.

--> 表的类型取决于您为应用程序计划的内容。它可能是 Innodb 或 Myisam。我建议您使用数字列,以便您可以增加/减少它们。如果您打算允许负数,请不要将其设为 UNSIGNED。

Here's the limits that you might find useful in declaring your column length:

以下是您在声明列长度时可能会发现有用的限制:

TINYINT (length)  - 1 - Integer with unsigned range of 0-255 and a signed range from -128-127
SMALLINT (length)  - 2 - Integer with unsigned range of 0-65535 and a signed range from -32768-32767
MEDIUMINT(length)  - 3 -  Integer with unsigned range of 0-16777215 and a signed range from -8388608-8388607
INT(length)   - 4 - Integer with unsigned range of 0-429467295 and a signed range from -2147483648-2147483647
BIGINT(length)   - 8 -  Integer with unsigned range of 0-18446744 and a signed range from
-9223372036854775808-9223372036854775807

2.is there a minimum version of MySQL I must use?

2.是否有我必须使用的最低版本的 MySQL?

--> Just to use auto-increment? You are well off using an up to date version. I suggest something > 5.2.4, if it's possible.

--> 只是为了使用自动增量?您可以使用最新版本。如果可能的话,我建议 > 5.2.4。

3.what's the sql code for this, safe transaction for MySQL?

3.mysql 安全事务的 sql 代码是什么?

--> Sorry, don't have an answer for this at the moment.

--> 抱歉,目前没有答案。