Building Queries

As discussed earlier in this chapter, you build queries using the CFQUERY tag and SQL.

Note To query the employees table:
  1. Return to HomeSite.
  2. Create a new application page.
  3. Select the Default Template for your page.
  4. Change the document title from Untitled to Employee List.
  5. Type Employee List directly under the begin BODY tag.
  6. Enclose Employee List in a H1 block tag.
  7. Add this code directly under the H1 tag to retrieve data from a database:
  8. <CFQUERY NAME="EmpList" DATASOURCE="HRExpress">
        SELECT FirstName,LastName, StartDate, Salary, Contract
        FROM Employees
    </CFQUERY>
    

    The DATASOURCE attribute that you enter here refers to the data source that you created.

  9. Save the page as EmpList.cfm in CFDOCS under the Web root directory.
  10. For example, the directory path on your machine may be: C:\INETPUB\WWWROOT\CFDOCS on Windows NT

    Or C:\WEBSHARE\WWWROOT\CFDOCS on Windows 95

    Your page should look like this:

    <HTML>
    <HEAD>
    <TITLE>Employee List</TITLE>
    </HEAD>
    <BODY>
    <H1>Employee List</H1>
    <CFQUERY NAME="EmpList" DATASOURCE="HRExpress">
        SELECT FirstName,LastName, StartDate, Salary, Contract
        FROM Employees
    </CFQUERY>
    </BODY>
    </HTML>
    
  11. Return to your browser and enter the following URL to view EmpList.cfm:
  12. http://127.0.0.1/CFDOCS/EmpList.cfm
    
  13. View source in the browser.
  14. The ColdFusion EmpList data set is stored with the page but only HTML and text is sent back to the browser.

Click here to see how the query page should look at this time.

Click here to see the code behind the scenes.

Note Note:HomeSite editing and visual tools make it easy to build queries. Refer to the Help tab in HomeSite to learn more about its features.