Skip to main content
February 2, 2012
Question

Referring to same cfquery multiple times returns different results

  • February 2, 2012
  • 4 replies
  • 1345 views

I'll try to slim this down a bit.

<cfquery year_loop>

<cfquery school_detail_counts_by_Year>

<cfloop query= "year_loop">

<cfloop query= "school_detail_counts_by_Year">

  output is fine, for each year, I get each set of school detail counts

</cfloop>

</cfloop>

Later below in same page I am essentially repeating this (for debugging), in reality I will still be looping through same two queries as above to make bar charts)

<cfloop query= "year_loop">

<cfloop query= "school_detail_counts_by_Year">

  output is ALWAYS values from LAST row in query  (YET I can display the data from YEAR_LOOP which shows it is changing)

</cfloop>

</cfloop>

Help, please help. I have no hair left!

This topic has been closed for replies.

4 replies

Participant
February 7, 2012

You could use cfdump to make sure the queries that you expect to be executing are indeed correct?

BKBK
Community Expert
Community Expert
February 7, 2012

Michael Rees wrote:

<cfloop query= "year_loop">

<cfloop query= "school_detail_counts_by_Year">

  output is fine, for each year, I get each set of school detail counts

</cfloop>

</cfloop>

Later below in same page I am essentially repeating this (for debugging), in reality I will still be looping through same two queries as above to make bar charts)

<cfloop query= "year_loop">

<cfloop query= "school_detail_counts_by_Year">

  output is ALWAYS values from LAST row in query  (YET I can display the data from YEAR_LOOP which shows it is changing)

</cfloop>

</cfloop>

Could you show us the code that is within the second set of cfloops. My guess is, that is where the problem occurs. The code is likely instructing ColdFusion to output just the last row!

Inspiring
February 7, 2012

If you don't call the query school_detail_counts_by_Year within the loop of <cfloop query= "year_loop"> again in the second set of loops it will only show the last row.

Inspiring
February 2, 2012

I suggest changing your approach so that you only have one query.  Running a query inside the loop of another one is inefficient and is adding unnecessary complexity to your code.  It should solve this specific problem as well.