简体   繁体   English

PHP Mysql-在Select语句中使用变量作为列名

[英]PHP Mysql - Use a variable as column name in Select statement

I have month names as column names such as jan, feb, march, etc. in my table. 我的表中有月份名称作为列名称,如jan,feb,march等。
In my Form user needs to choose a month from drop down which I take the user's selection as $month_name. 在我的表单中,用户需要从下拉列表中选择一个月,我将用户的选择作为$ month_name。

I want to see the data in $month_name which is not 0.00. 我想在$ month_name中查看不是0.00的数据。

When I echo $month_name I get: jan or feb or etc. 当我回显$ month_name时,我得到:jan或feb等。

I used $month_name in my select statement but not working: 我在选择语句中使用了$ month_name,但没有用:

1st try not working: 第一种尝试不起作用:

$sql = "SELECT * FROM bookoff_monthly WHERE '.$month_name.' <> 0.00 AND year='$year' ";

2nd try not working: 第二次尝试不起作用:

$sql = "SELECT * FROM bookoff_monthly WHERE $month_name <> 0.00 AND year='$year' ";

** but the following works but this isn't what I want: **,但是以下方法有效,但这不是我想要的:

$sql = "SELECT * FROM bookoff_monthly WHERE jan <> 0.00 AND year='$year' ";

This is what I would consider properly escaped: 我认为这是正确的转义:

$sql = "SELECT * FROM bookoff_monthly WHERE `".$month_name."` <> 0.00 AND year='".$year."' ";

Notice that around $month_name you have the backticks in the statement, then double quotes (as the entire query is surrounded by them) then the PHP concatenation ( . ). 注意,在$month_name周围,​​语句中有反引号,然后加双引号(因为整个查询都被双引号引起来),然后是PHP串联( . )。 You're better off thinking about it as a string + variable + string ... rather than a variable in a string. 您最好将其视为string + variable + string ...而不是string + variable + string ...的变量。 Same with the $year variable. 与$ year变量相同。

In your first statement you were close but you were using single quotes to break out of the string, but you can't do that if the string is contained in double quotes. 在您的第一个语句中,您很接近,但是您使用单引号将字符串引起来,但是如果字符串包含在双引号中,则不能这样做。

$var = "is a";

echo("This ".$var." string 'with quoted text'"); // This is a string 'with quoted text'

echo("This ".$var." string \"with quoted text\""); // This is a string "with quoted text"

echo('This '.$var.' string "with quoted text"'); // This is a string "with quoted text"

echo('This '.$var.' string \'with quoted text\''); // This is a string 'with quoted text'

Assuming your variables are in the correct format then this should work. 假设您的变量采用正确的格式,那么这应该起作用。

EDIT: As VMai says, because you are dealing in 3 letter month names, you will inevitably be using "DEC" or "dec" in this query, and that is a reserved word in MySQL so you must escape the name with backticks or the query will be misinterpreted. 编辑:正如VMai所说,因为您要处理3个字母月份的名称,所以您将不可避免地在此查询中使用“ DEC”或“ dec”,并且这是MySQL中的保留字,因此您必须使用反引号或查询将被曲解。

As to the direct question: 至于直接的问题:

The problem with #1 is that you are trying to mix single and double quotes. #1的问题是您要混合使用单引号和双引号。 Assuming that $month_name = "jan", this will give you: 假设$ month_name =“ jan”,这将为您提供:

SELECT * FROM bookoff_monthly WHERE '.jan.' <> 0.00 AND year='$year'

As '.jan.' 为“ .jan”。 is not equal to 0.00, this won't find any records. 不等于0.00,将找不到任何记录。

Your second one looks to me like it should work. 您的第二个在我看来应该可以使用。 You might try printing the resulting string to see what it's giving you. 您可以尝试打印结果字符串以查看它为您提供了什么。 Perhaps you have additional characters you're not expecting in the month_name or something. 也许您在month_name或其他内容中没有其他字符。

But in any case, this is just asking for SQL injection attacks. 但是无论如何,这只是在请求SQL注入攻击。 A hacker could, for example, fake out your input form and set jan="1+1=2; delete from bookoff_monthly where 1.00 ", and nowit will delete everything for the given year. 例如,黑客可以伪造您的输入表单并设置jan =“ 1 + 1 = 2;从bookoff_monthly其中1.00”处删除,现在它将删除给定年份的所有内容。

Plus any design that has you generating field names on the fly should be questioned right off the bat. 再加上任何让您即时生成字段名称的设计都应立即受到质疑。

The right way to do this is to have a many-to-may relationship with one record for each month. 正确的方法是与每个月有一个记录保持多对多的关系。 Like create table bookoff_month (bookoff_id int, month char(3), amount decimal(7,2)). 就像创建表bookoff_month(bookoff_id int,月份char(3),金额小数(7,2))一样。 Then your query becomes select * from bookoff_monthly join bookoff_month on bookoff_month.bookoff_id=bookoff_monthly.bookoff_id where month=? 然后您的查询将成为select * from bookoff_monthly加入bookoff_month on bookoff_month.bookoff_id = bookoff_monthly.bookoff_id其中month =? and year=? 和年份=? and fill them in with a prepared statement, or at least wrap the parameters in a function that does proper escaping. 并用准备好的语句填充它们,或者至少将参数包装在进行适当转义的函数中。

$sql = 'SELECT * FROM bookoff_monthly WHERE '.$month_name.' <> 0.00 AND year="'.$year.'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM