在 MySQL UPDATE (PHP/MySQL) 中使用变量

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

Using variables in MySQL UPDATE (PHP/MySQL)

phpmysqlsqlvariablessql-update

提问by MU_FAM

I am using this code so I can update a record in database:

我正在使用此代码,以便可以更新数据库中的记录:

$query = mysql_query("UPDATE article 
                         SET com_count = ". $comments_count 
                       WHERE article_id = .$art_id ");

My question is: How can I use variables in a MySQL UPDATE statement.

我的问题是:如何在 MySQL UPDATE 语句中使用变量。

回答by morgar

$query = mysql_query("UPDATE article set com_count = $comments_count WHERE article_id = $art_id");

$query = mysql_query("UPDATE article set com_count = $comments_count WHERE article_id = $art_id");

You was messing up the quotes and concats.

你弄乱了引号和连接符。

You can use inline vars like the previous example or concat them like:

你可以像前面的例子一样使用内联变量,或者像这样连接它们:

$query = mysql_query("UPDATE article set com_count = " . $comments_count . " WHERE article_id = " . $art_id);

$query = mysql_query("UPDATE article set com_count = " . $comments_count . " WHERE article_id = " . $art_id);

回答by dee-see

You messed up on your " .pattern.

你搞砸了你的" .模式。

$query = mysql_query("UPDATE article set com_count = ". $comments_count . " WHERE article_id = " . $art_id . ");

回答by Fahim Faysal

Use apostrophes when using variables in a MySQL UPDATE statement:

在 MySQL UPDATE 语句中使用变量时使用撇号:

$query = mysql_query("UPDATE article 
                      SET com_count =  '$comments_count'
                      WHERE article_id = '$art_id'");

Be careful about space and apostrophes.

注意空格和撇号。