Skip to main content
Participant
July 24, 2010
Answered

simple cfquery question - amateur level

  • July 24, 2010
  • 1 reply
  • 963 views

Hello all,

making some simple changes to my website

there is already a database running with plenty of cfloop query

question is this:

this code below retrieves the cake name (#cake_name#) for row 1 in the database, but its using a loop

<cfloop query="cake_query" STARTROW="1" ENDROW="1">

#cake_name#

</cfloop>

how do i do this without using a loop?

i thought it ould be as simple as this:

<cfquery="cake_query" ROW="1">

</cfquery>

but apparently not...

also at the top of the code, there is already this code below, which i know works with the loop queries, im assuming i dont need to change for single row queries?

<cfquery name="cake_query" datasource="#request.datasource#"
username="#request.username#" password="#request.password#">
SELECT * FROM gce_cakes where cake_active = 1 ORDER BY cake_number
</cfquery>

Thank you in advance to anyone that can help

This topic has been closed for replies.
Correct answer Adam Cameron.

If you want to access any given element of a query, irrespective of how many rows it has, the format is:

queryName.columnName[rowNumber]

And if you want to output it:

<cfoutput>#queryName.columnName[rowNumber]#</cfoutput>

As for the <cfquery> question... <cfquery> is for sending SQL strings and params to a DB driver, and converting what it returns into a recordset (misguidedly referred to as a "query" quite often in CF parlance & tag attributes).  <cfquery> doesn't care how many rows are returned from the DB: 0... 1... more than one... it just returns a recordset with that many rows in it.

Make sense?


--

Adam

1 reply

Adam Cameron.Correct answer
Inspiring
July 24, 2010

If you want to access any given element of a query, irrespective of how many rows it has, the format is:

queryName.columnName[rowNumber]

And if you want to output it:

<cfoutput>#queryName.columnName[rowNumber]#</cfoutput>

As for the <cfquery> question... <cfquery> is for sending SQL strings and params to a DB driver, and converting what it returns into a recordset (misguidedly referred to as a "query" quite often in CF parlance & tag attributes).  <cfquery> doesn't care how many rows are returned from the DB: 0... 1... more than one... it just returns a recordset with that many rows in it.

Make sense?


--

Adam

Participant
July 24, 2010

Adam,

Thank you! your code has made perfect sense and works.

also what you said about cfquery sort of makes sense, im hoping i can get by on my LIMITED knowledge till i can afford to

get the wholesite rebuilt professionally.

Thanks again, you have really saved me hours of time.