Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

coldfusion - Getting complex object error when trying to output query values

My goal is to just output the column data specified in the "fieldList".

Getting the following error:

Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a cfif tag. The error occurred on line 20.

When trying to do the following:

<cfquery datasource="retailers" name="myQuery">
Select * FROM retailer 
WHERE retailer_id = '#url.id#'
</cfquery>

<cfset fieldList = "company,phone,phone_secondary,fax,email,website">
<cfloop list="#fieldList#" index="i">      
#myQuery[i]#
</cfloop>

Shouldn't this work without giving me an error? I feel like I'm just overlooking something simple I just can't find the answer anywhere.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, I see you ammended your initial code, so here's my response:

You are looping over a list of columns, and trying to evaluate each column as though it is a single element in a struct.

A query, however, is slightly different: it is accessed as a series of structs, which in turn, are arrays--of each row of data as they return from the query.

If you want to output all of the rows of data, without knowing the columns up front (or passing them in dynamically as you are implying in your code above), try this:

<cfoutput query="myQuery">
  <cfloop list="#myQuery.ColumnList#" index="thisColumn">
    #myQuery[thisColumn][myQuery.CurrentRow]#
  </cfloop>
</cfoutput>

This will provide you with the output you need. The outer cfoutput with the query attribute will loop over all the rows of data you received. Then, for each row, you loop over the list of columns the query produces, and for each column, output the data, specifying the row via myQuery.CurrentRow, which iterates automatically for you via the outer output.

I'd also take a moment now to advocate that you try to stay away from loops within loops, and just output your values explicitly:

<cfoutput query="myQuery">
  #company# #phone#
</cfoutput>

Using this syntax is slightly less expensive than the aforementioned loop within a loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...