当查询列是变量时从cfquery获取值

ear*_*efl 4 coldfusion cfquery cfloop

我被困了......不记得如何使这项工作.代码:

<cfquery name = "getsomething>
   select a, b, c
   from d
   where id = '1'
</cfquery>
<cfloop collection="#arguments#" item="argument">
    <cfif #StructFind(arguments, argument)# neq #getsomething.argument[0]#><!--- here's the problem --->
        do something
    </cfif>
</cfloop>
Run Code Online (Sandbox Code Playgroud)

查询返回一条记录; 我需要获取该记录的每列的值.列名是变量(参数).我需要替换什么语法

 #getsomething.argument[0]#
Run Code Online (Sandbox Code Playgroud)

?谢谢.

Sha*_*mes 7

你需要做一些调整:

我看到你正在使用"集合"参数进行循环.这意味着你有一个像这样的数据结构:

<cfset arguments = StructNew() />
<cfset arguments.a = 'x' />
<cfset arguments.b = 'y' />
<cfset arguments.c = 'c' />
Run Code Online (Sandbox Code Playgroud)

你可以看到,值不会在这种情况下,重要 - 重要的是,通过使用"收藏"的说法,您使用的是结构的工作.有点不正确,但让我们继续前进的假设.

你不希望动态评估参数的值,你想要键 -它们映射到你的列,所以像这样循环:

<cfloop list="#StructKeyList(arguments)#" index="argument">
Run Code Online (Sandbox Code Playgroud)

那么,以下代码有效:

<cfif StructFind(arguments, argument) neq getsomething[argument][1]>
Run Code Online (Sandbox Code Playgroud)

请注意,在这个答案中,我已将查询索引从0更改为1:cfquery数组是从1开始的,因此第一行不是[0],而是[1].