I'm trying to do a MySQLi query in PHP but the name of one of the columns is stored in a variable ($year
):
我正在尝试在PHP中执行MySQLi查询,但其中一列的名称存储在变量($ year)中:
$result = mysqli_query($con,"SELECT table4.Country, AVG('.$year.') AS 'Total'
FROM table4
WHERE table4.Region IN ($region)");
This query returns 0 as average value. I think that the problem could be that $year
is a number and I don't know if a column can have an integer as name.
此查询返回0作为平均值。我认为问题可能是$ year是一个数字,我不知道列是否可以有一个整数作为名称。
How can I solve this problem?
我怎么解决这个问题?
Thanks in advance!
提前致谢!
1 个解决方案
#1
2
Your code is not proper. As per your code MySQL will try to find the AVG('.$year.') which is not a valid expression. This needs to be changed.
你的代码不合适。根据你的代码,MySQL会尝试找到AVG('。$ year。'),这不是一个有效的表达式。这需要改变。
So, assuming the $year contains field name which is non-integer, Try this below code:
因此,假设$ year包含非整数字段名称,请尝试以下代码:
$result = mysqli_query($con,"SELECT table4.Country, AVG(`".$year."`) AS 'Total' FROM table4 WHERE table4.Region IN ($region)");
Hope this helps.
希望这可以帮助。
#1
2
Your code is not proper. As per your code MySQL will try to find the AVG('.$year.') which is not a valid expression. This needs to be changed.
你的代码不合适。根据你的代码,MySQL会尝试找到AVG('。$ year。'),这不是一个有效的表达式。这需要改变。
So, assuming the $year contains field name which is non-integer, Try this below code:
因此,假设$ year包含非整数字段名称,请尝试以下代码:
$result = mysqli_query($con,"SELECT table4.Country, AVG(`".$year."`) AS 'Total' FROM table4 WHERE table4.Region IN ($region)");
Hope this helps.
希望这可以帮助。