Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a query row by index in ColdFusion?

I want to get a specific row in a ColdFusion Query object without looping over it.

I'd like to do something like this:

<cfquery name="QueryName" datasource="ds"> SELECT * FROM    tablename </cfquery>  <cfset x = QueryName[5]> 

But it's giving me an error saying that the query isn't indexable by "5". I know for a fact that there are more than 5 records in this query.

like image 611
Brian Bolton Avatar asked Jul 31 '09 13:07

Brian Bolton


2 Answers

You can't get a row in CF <= 10. You have to get a specific column.

<cfset x = QueryName.columnName[5]> 

It's been 8 years since I posted this answer, however. Apparently CF11 finally implemented that feature. See this answer.

like image 83
Patrick McElhaney Avatar answered Sep 24 '22 01:09

Patrick McElhaney


This can now be accomplished in coldfusion 11 via QueryGetRow

<cfquery name="myQuery" result="myresult" datasource="artGallery" fetchclientinfo="yes" > select * from art where ARTID > <cfqueryparam value="2" cfsqltype="CF_SQL_INTEGER"> </cfquery>  <cfdump var="#myQuery#" >  <cfset data = QueryGetRow(myQuery, 1) >  <cfdump var="#data#" > 
like image 32
ugh StackExchange Avatar answered Sep 24 '22 01:09

ugh StackExchange