ColdFusion 4.0 Primer

About Databases

The database is the backend of any CF website. For the purposes of this tutorial we will be using Microsoft Access. Microsoft Access is a relational database which means you can relate the tables to one another. Now this article assumes you know how to work a database and are familiar with basic terms such as Field, Row, and Table. In truth MS Access isn't the best database to run a website off of, however it is likely the only database most people have access to and it makes a good learning tool.

When accessing a relational database from a website regardless if its Oracle, Microsoft, or MySQL, and regardless if you're using CF, PHP, or ASP, you will use a language called SQL -- Structured Query Language. SQL is the bridge between the frontend of your website and the backend. In this article you will be shown how to construct and use basic SQL statements which can be applied to any server side scripting language, not only CF.

To let CF access a database you will need to set up a DSN, Data Source Name, on your server. To do this first upload the database file and then tell your server administrator you need a DSN set up with XXXX name using Microsoft Access Drivers, and tell them the location of the database file. Depending on which platform and or database you are using this step might vary.

Basic ColdFusion

For starters name all of your CF files with a .cfm extension. ColdFusion is a lot like HTML in that it uses tags to define many functions, this may make it easier to learn. Look at the following Example:
Figure 1. Form.html
<form method = "post" action = "name.cfm"> 
<input type = "text" size = "20" name = "name"> 
<input type = "submit" value = "Submit"> 
</form>
Figure 2. Name.cfm
<CFOUTPUT> 
Hello, #name#, Welcome to my website! 
</CFOUTPUT>

In name.cfm the first tag you see is a <CFOUTPUT> tag. This tag can be put anywhere in a webpage, it can be put in the middle of an HTML tag it does not matter, what this does if tell the CF interpreter that you are going to display a variable. The variable itself is enclosed in #'s which is how CF marks its variables. This causes a problem in some instances when you use #'s on your website, such as when declaring colors. This is very simple to fix, you can simply move the <CFOUTPUT> tags directly to the sides of the variable as in the following example.

Figure 3. Name.cfm
Hello, <CFOUTPUT #name#</CFOUTPUT>, Welcome to my website!

You can also simply put a double ## when you need to use a pound sign for another purpose. Finally we see the </CFOUTPUT> tag, and just like HTML you need an end tag for most CF tags.