Introducing SQL Queries
Now that you've configured your database on the server and enabled ColdFusion to see it, you're ready to start using this data in your site. In the last two tasks of this lesson, you'll retrieve data from the database and then output it into a table. To access the data from the database, the ColdFusion document needs to communicate with the database. But there's a catch: databases don't understand ColdFusion Markup Language (CFML).The language that modern databases understand is SQL, which is pronounced "sequel" and stands for Structured Query Language. ColdFusionlike ASP, PHP, and JSPuses SQL to obtain information from a database, just like a database programmer would. After that data is obtained, it can be manipulated and/or displayed on the page. Serious application developers need to learn quite a bit of SQL to build powerful applications. But thanks to Dreamweaver's dialog-based ways to construct SQL statements (the scripts used to access databases and the information stored in them), nonprogrammers and SQL newbies can construct SQL queries fairly easily. In this task, you'll create a bona fide SQL statement using a Dreamweaver dialog that will mask what you're doing.
1. | Open dante_admin.cfm in Dreamweaver. |
You'll eventually output database data into this page. Before you can do that, you'll have to configure Dreamweaver to see ColdFusion DSNs. You'll need to configure Dreamweaver only once per site, and you can have any page within the site open when you do it.
2. | Choose Window > Server Behaviors. Click the fourth link in the panel, entitled Specify the RDS Login.... In the pop-up dialog that appears, enter your password. |



Dreamweaver's Databases panel clearly provides convenient access to your data. Not only can you see the database schema (the structure of the tables and their fields), but you can even view its data right in Dreamweaver.[View full size image]

4. | Choose Window > Bindings to open the Bindings panel. |
You use the Bindings panel as a resource for adding dynamic data to a page. In Lesson 13, you used the Bindings panel to specify a form variable (Form.firstName). This time, you're going to create a recordset.The recordset you create will contain all the data in the users table, except each user's password and the primary key (unique ID).
5. | Click the + button near the top of the Bindings panel, and choose Recordset (Query) from the menu. |

6. | In the Recordset dialog, enter rs_GetUsers as the Name, and choose dante from the Data source drop-down menu. |
The name can be anything you want. Many developers preface their recordset names with rs_ so they're easily recognized in code.To build a recordset, you have to specify the data. After you tell ColdFusion which DSN to use, you'll once again have access to all the tables and fields in the dante.mdb database, just as you did in the Insert Record dialog.
7. | Choose users from the Table drop-down menu. When the data refreshes, click the Selected radio button in the Columns category, and Ctrl-select (Windows) or Command-select (Macintosh) the username, firstName, and lastName fields in the Column area. |
In this step, you're telling ColdFusion to build the recordset from only data in the username, firstName, and lastName fields. All the data in the other fields is ignored.
8. | In the first Sort drop-down menu, choose lastName, and in the second field choose Ascending. |
These settings tell ColdFusion how to sort the information in the recordset. In this case, you're sorting by last name.The final dialog should appear as in the following figure.[View full size image]

9. | Before clicking OK, click the Test button. |
Clicking the Test button causes Dreamweaver to show the results of the recordset you're building. This is a great way to test your SQL query without having to save the page, upload it, and test it.You should see 13 records, listed alphabetically by last name.[View full size image]

10. | Click OK to exit the Test window, and OK again to exit the Recordset dialog. |
The Bindings tab now lists Recordset (rs_GetUsers). If you expand the recordset, you'll see username, firstName, and lastName listed with lightning-bolt icons.

11. | Switch to code view and look at the code at the top of the document. |
You'll see the following code:
<cfquery name="rs_GetUsers" datasource="dante">Sandwiched between the <cfquery> tags is the SQL statement you built in the Recordset dialog. The <cfquery> tags, as you've probably guessed, tell ColdFusion to send a query to the database. The query is the SQL statement. The recordset's name and the DSN are both passed as parameters of the <cfquery> statement.As we mentioned at the beginning of this lesson, the beauty of DSNs is that you can change their particulars all you want (for example, if you migrate to a different database or server) without breaking any pages, because the pages reference only the DSN name itself.[View full size image]
SELECT username, firstName, lastName
FROM users
ORDER BY lastName ASC</cfquery>
