Getting value from cfquery when query column is variable
I'm stuck ... can't remember how to do this. 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 the problem ---> do something </cfif> </cfloop> The query returns one record; I need to get the values ββof each column for this entry. The column name is a variable (argument). What syntax do I need to replace
#getsomething.argument[0]# ? Thanks.
You need to make a couple of settings:
I see that you are looping using the "collection" argument. This means that you have a data structure, for example:
<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 you use the collection argument with the structure. A bit wrong, but let's move forward with an assumption.
You do not want the value of your arguments to be dynamically evaluated; you want the keys - they matched with your columns, so the loop looks 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 changed the index of queries from 0 to 1: cfquery arrays are based on 1, so the first line is not [0], but [1].