Getting a complex object error while trying to output query values

My goal is to simply output the column data specified in the Field field.

Getting the following error:

Complex object types cannot be converted to simple values. The expression requested the variable or result of the intermediate expression as a simple value, but the result cannot be converted to a simple value. Simple values ​​are strings, numbers, booleans, 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 simple. For example, you can use the query variable in the cfif tag. An 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> 

Doesn't this work give me a mistake? I feel like I’m just forgetting something simple, I just can’t find the answer anywhere.

+6
source share
1 answer

Ok, I see that you fixed your source code, so here is my answer:

You iterate over the list of columns and try to evaluate each column as if it were the only element in the structure.

However, the query is slightly different: it is referred to as a series of structures, which, in turn, are arrays of each row of data as they return from the query.

If you want to display all rows of data without knowing the columns in front (or dynamically passing them as you mean in your code above), try the following:

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

This will give you the result you need. An external cfoutput with a request attribute will iterate over all the rows of data that you received. Then, for each row, you iterate over the list of columns created by the query, and for each column, output the data by specifying a row through myQuery.CurrentRow, which automatically iterates through external output.

I would also take a moment to protect you from trying to avoid loops inside loops and just display your values ​​explicitly:

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

Using this syntax is slightly cheaper than the above loop in a loop.

+8
source

Source: https://habr.com/ru/post/910291/


All Articles