Getting value from cfquery when query column is a variable

1.6k views Asked by At

I'm stuck... can't remember how to make this work. The code:

<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>

The query returns one record; I need to get the values of each column for that record. The column name is a variable (argument). What syntax do I need to replace

 #getsomething.argument[0]#

? Thanks.

1

There are 1 answers

4
Shawn Holmes On BEST ANSWER

You need to make a couple of adjustments:

I see you are looping using the "collection" argument. This implies you have a data structure like so:

<cfset arguments = StructNew() />
<cfset arguments.a = 'x' />
<cfset arguments.b = 'y' />
<cfset arguments.c = 'c' />

You can see that the values do not matter in this case--what matters is that by using the "collection" argument, you are working with a struct. Somewhat incorrect, but let's move forward with the assumption.

You do not want the value of your arguments dynamically evaluated, you want the keys--they map to your columns, so loop like this:

<cfloop list="#StructKeyList(arguments)#" index="argument">

then, the following code works:

<cfif StructFind(arguments, argument) neq getsomething[argument][1]>

Note that in this answer, I've changed your query index from 0 to 1: cfquery arrays are 1-based, so the first row is not [0], but [1].