You can query databases to retrieve data at runtime. The retrieved data, called the result set, is stored on that page as a query object. When retrieving data from a database, perform the following tasks:
cfquery
tag on a page to tell ColdFusion how to connect to a database.
cfquery
block to specify the data that you want to retrieve from the database.cfoutput
, cfgrid
, cftable
, cfgraph
, or cftree
.
The cfquery
tag is one of the most frequently used CFML tags. You use it in conjunction with the cfoutput
tag so that you can retrieve and reference the data returned from a query.
When ColdFusion encounters a cfquery
tag on a page, it does the following:
cfquery
tag's name
attribute. Often, we refer to the query object simply as "the query".<cfquery name="EmpList" datasource="CompanyInfo"> You'll type SQL here </cfquery>
In this example, the query code tells ColdFusion to:
CompanyInfo
data source to connect to the company.mdb
database.
Follow these rules when creating a cfquery
tag:
cfquery
tag is a block tag, that is, it has an opening <cfquery
> and ending cfquery
> tag.
name
attribute to name the query object so that you can reference it later on the page.datasource
attribute to name an existing data source that should be used to connect to a specific database. Alternatively, use the dbtype = "dynamic"
and connectString
attributes to dynamically specify a database.cfquery
block to tell the database what to process during the query.Select * from mytable WHERE FirstName='Russ'
selects every record from mytable in which the first name is Russ.