Retrieving Records From a Database..

This tutorial will demonstrate how to query a database and then display them on your site in order.

When you need to retrieve and display records from a database, ColdFusion is the easiest and fastest way to do so, simply because it takes less coding then all other developers languages to do so.  In this tutorial I will demonstrate how to query a database using <CFQUERY> and then display the records using <CFOUTPUT>.  Let's begin:

The first step that is involved is querying the database for all of the records that exist.

<!--- Query The Database For All Records In That Table --->
<CFQUERY NAME="GetRecords" DATASOURCE="YourDSN">
   SELECT          *
   FROM             TableName
   ORDER BY      FieldName
</CFQUERY>

<CFOUTPUT QUERY="GetRecords">
   #GetRecords.CurrentRow#)  #FieldName# - #FieldDescription#<BR>
</CFOUTPUT>

That code above will query your database for all records in the table "TableName" and return them to be displayed on your page.  Notice that I simply put the name of the fields within that table and surrounded them with # signs.  The # signs tell ColdFusion to process for the values when they are within a <CFOUTPUT> tag.  Another thing I did was use the code:
#GetRecords.CurrentRow#, this simply let's ColdFusion server that you want the number of the current record being displayed, that way you can show your results in order with numbers to the left as in the example below.

1) Pablo - This is User Number 1
2) Jessica - This is User Number 2
3) Tony - This is User Number 3

That is how easy and fast you can begin using ColdFusion to make your database interact with your web pages.



All ColdFusion Tutorials By Author: Pablo Varando