php 将 1 添加到字段

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

Add 1 to a field

phpmysql

提问by Teifion

How do I turn the following 2 queries into 1 query

如何将以下 2 个查询转换为 1 个查询

$sql    = "SELECT level FROM skills WHERE id = $id LIMIT 1;";
$result = $db->sql_query($sql);
$level  = (int) $db->sql_fetchfield('level');
$db->sql_freeresult($result);

++$level;

$sql    = "UPDATE skills SET level = $level WHERE id = $id;";
$result = $db->sql_query($sql);
$db->sql_freeresult($result);

I'm using it in a phpBB mod but the gist is that I grab the level, add one to it then update, it seems that it'd be much easier and faster if I could do it as one query.

我在 phpBB mod 中使用它,但要点是我获取级别,添加一个然后更新,如果我可以将它作为一个查询来执行,它似乎会更容易和更快。

Edit: $idhas already been forced to be an integer, thus no escaping is needed this time.

编辑:$id已经被强制为整数,因此这次不需要转义。

回答by Josh

I get downmodded for this?

我因此而被贬低?

$sql = "UPDATE skills SET level = level+1 WHERE id = $id";
$result = $db->sql_query($sql);
$db->sql_freeresult($result);

In Teifion's specific case, the phpBB DDL lists that particular field as NOT NULL, so there's no danger of incrementing NULL.

在 Teifion 的特定情况下,phpBB DDL 将该特定字段列为 NOT NULL,因此没有增加 NULL 的危险。

In the general case, you should not use NULL to represent zero. Incrementing NULL shouldgive an answer of NULL. If you're the kind of misguided developer who thinks NULL=0, step away from keyboard and find another pastime, you're just making life hard for the rest of us. Of course, this is the computer industry and who are we to say you're wrong? If you're not wrong, use

在一般情况下,您不应使用 NULL 来表示零。递增 NULL应该给出NULL的答案。如果您是那种认为 NULL=0 的被误导的开发人员,请远离键盘并寻找另一种消遣,您只会让我们其他人的生活变得艰难。当然,这是计算机行业,我们凭什么说你错了?如果你没有错,请使用

$sql = "UPDATE skills SET level = COALESCE(level,0)+1 WHERE id = $id";

...but let's face it: you're wrong. If everyone starts at level 0, then your DDL should include

...但让我们面对现实:你错了。如果每个人都从 0 级开始,那么你的 DDL 应该包括

level INT DEFAULT '0' NOT NULL

in case the programmers forget to set it when they create a record. If not everyone starts on level 0, then skip the DEFAULT and force the programmer to supply a value on creation. If some people are beyond levels, for whom having a level is a meaningless thing, then adding one to their level equally has no meaning. In that case, drop the NOT NULL from the DDL.

以防程序员在创建记录时忘记设置它。如果不是每个人都从 0 级开始,则跳过 DEFAULT 并强制程序员在创建时提供一个值。如果有的人是超层次的,有层次对他来说是没有意义的,那么同样的在层次上加一个也没有意义。在这种情况下,从 DDL 中删除 NOT NULL。

回答by Lasse V. Karlsen

This way:

这边走:

UPDATE skills
SET level = level + 1
WHERE id = $id

回答by Imran

With PDO and prepared query:

使用 PDO 和准备好的查询:

$query = $db->prepare("UPDATE skills SET level = level + 1 WHERE id = :id")
$query->bindValue(":id", $id);
$result = $query->execute();

回答by Mat

$sql = "UPDATE skills SET level = level + 1 WHERE id = $id";

$sql = "更新技能SET level = level + 1 WHERE id = $id";

I just hope you are properly sanitising $id elsewhere in your code!

我只希望您在代码中的其他地方正确清理 $id !

回答by Karl Seguin

try this

尝试这个

UPDATE skills SET level = level + 1 WHERE id = $id

回答by Sean Carpenter

How about:

怎么样:

UPDATE skills SET level = level + 1 WHERE id = $id;

回答by Josh

Mat: That's what pasted in from the question. It hasn't been edited, so I attribute that to a bug in Markdown. But, oddly enough, I have noticed.

Mat:这就是问题中粘贴的内容。它尚未被编辑,所以我将其归因于 Markdown 中的一个错误。但是,奇怪的是,我注意到了。

Also: yes, mysql_escape_string()!

还有:是的,mysql_escape_string()