gpt4 book ai didi

php mysql_query 使用变量作为字段名称

转载 作者:行者123 更新时间:2023-11-29 22:50:42 26 4
gpt4 key购买 nike

我知道我不应该使用 mysql_query 进行数据库查询,但我需要修改现有代码。我需要做的是将 php 变量作为 sql 查询的字段名称传递。我尝试过这样的方式:

$my_field = "field_name";
mysql_query("UPDATE my_table SET ".$my_field." =somevalue") or die(mysql_error());

但我注意到这是错误的,因为结果查询是

UPDATE my_table SET  =somevalue

正确的做法是什么?

最佳答案

您错过了收盘价,请更改为:

mysql_query("UPDATE my_table SET ".$my_field." =somevalue") or die(mysql_error());

为了检查,将语句添加到变量并回显它,如下所示:

$my_field = "field_name";
$query = "UPDATE my_table SET ".$my_field." =somevalue");
echo $query; //see the output to check if it shows correct statement

关于php mysql_query 使用变量作为字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com