The ColdFusion language falls into one of two categories depending on who you are.
If you're an HTML god who has difficulty conversing with other Human beings without saying close tag at the end of every sentence there is a good chance that you'll like ColdFusion.
If on the other hand you prefer to talk to people about floating point numbers and just how useful 2 dimensional arrays are than you might find learning ColdFusion a bit of a drag.
It all depends on how you like to look at logical ways of doing things. The ColdFusion Mark-up Language or CFML is based on tags.
The following code would retrieve information from a database and output it in a table. I'll explain each piece of code later.
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
4 <title>My First ColdFusion Template</title>
7 <cfquery name=myquery datasource=MySystemDSN>
8 SELECT * FROM mytable
10 11 <cfoutput query="myquery">
Look Puzzling? It shouldn't do! At least, not too puzzling. Let's break down what we have here.
This shouldn't be anything new; as this is standard HTML setting up the document as you would any other Web page.
This is the first piece of ColdFusion code you encounter. The cfquery tag is the basis for any retrieval of data from a database.
First of all you have to call your query something so that you can access the data that it retrieves.
Second, you open the connection to a database, I use the Windows version of ColdFusion server so in the same way that Active Server Pages connects to a database, you create a system Datasource and point that at your Database. You then tell ColdFusion to connect to that Datasource
<cfquery name=myquery datasource=MySystemDSN>
Thirdly you need to write an SQL statement to get the records out of the database you want. Please note that this is a Structured Query Language statement, not ColdFusion, if you want more info on SQL come back soon to thescripts.com and checkout the new ColdFusion articles where I'll do some more SQL.
SELECT * FROM mytable
This statement will Select all (by using
*) from a table called mytable. More complicated SQL will be covered in later tutorials
.Last but not least you have to close the cfquery tag.
The cfquery tag is far more powerful than this, it has more options and attributes and I've only showed you a small amount to help you understand what it's about.
This is a mixture of ColdFusion and HTML but the tag we really want to look at is cfoutput tag as this is what you have to use if you want to output any kind of ColdFusion enhanced data. cfoutput can be used in its very basic form in the form of:
However, when it's tied with a query as detailed above it becomes a lot more powerful. It will allow you to access all of the data from that query. In our example this means any of the fields in "mytable". When used like this cfoutput will get all of the variables from one record and then go onto the next record.
21 <cfoutput query="myquery">
The values that are inside the Pound symbols
## (I call them hash symbols because I come from the UK but there you go) represent variables, hence when the code is execute they are replaced by the value of that variable.
In this example the query we did earlier got all of the records from mytable, in that table there were three columns, fldone, fldtwo and fldthree, we access the data in there using variables. E.g.
So this code would take the values of fldone fldtwo and fldthree of the first record place them into the table columns, and then repeat for record two until all records were output.
See, easy isn't it?