ColdFusion Power Guide


 

This Month PC User brings you the 10 latest tips and tricks for Allaire's Cold Fusion.


Use Query Caching

Database queries are usually the slowest part of a ColdFusion application. If your data doesnÆt constantly change, you can cache the results of queries for a big performance boost. The easiest way to do this is by using the ôcachedwithinö attribute in the <cfquery> tag.

Example:

<cfquery name=öMyQueryö

  datasource=öMyDSö

  cachedwithin=CreateTimeSpan(0,1,0,0)>

 

Note the CreateTimeSpan() function, which specifies the cache time in days, hours, minutes and seconds. The query above will be cached for 1 hour.


Extra Query Information

ColdFusion returns extra variables with every database query that can be very useful in your code:

Variable Name

Value

Queryname.RecordCount

Number of records returned from query

Queryname.CurrentRow

Current query row being displayed (used in loops)

Queryname.ColumnList

Comma separated list of column names

CFQUERY.ExecutionTime

Query Processing time in milliseconds

 


Customised error handling

ColdFusion automatically displays and logs all errors, but the error messages are technical and designed for developers to fix bugs. Follow these steps to create a more user friendly error page:

1.      Create a standard ColdFusion page to use for reporting errors.

2.      Login to the ColdFusion Administrator.

3.      Choose Settings from the menu

4.      Enter a file path to your template under Site-wide Error Handler (eg. C:\myfolder\myerrorpage.cfm)

5.      Press Apply


Reuse common code

You can use the <cfinclude> tag to reuse common code within your ColdFusion templates. This allows you to create a single template for things such as headers and navigation bars and then reuse that code wherever itÆs required, which greatly simplifies maintenance.


Check browser version

You can check which browser people are using by testing the variable cgi.http_user_agent.

Example:

<cfif cgi.http_user_agent contains ôMSIEö>

   You are using Internet Explorer

</cfif>


Turn on Debugging Information

Turn on debugging information to display the value of variables and queries in your templates. ItÆs a good idea to always use debugging information during development:

1.      Login to the ColdFusion Administrator

2.      Select ôDebuggingö from the menu

3.      Press Apply


Using the # symbol

ColdFusion uses the # symbol to indicate variables and functions. If you want to insert the # symbol into your code, just write it twice.

 

Example:

<body bgcolor=ö##000000ö>


Application.cfm

If you create a template called Application.cfm, it will be automatically included at the top of every page in its directory and below. This template is a good place to perform security checks or to define variables that are used in many places.


Remove special characters in URL variables

There are a number of characters that canÆt be placed in a URL variable, such as a space. Use the URLEncodedFormat() function to automatically escape special characters.

 

Example:

<A HREF=önextpage.cfm?search=#URLEncodedFormat(searchtext)#ö>


Dynamic Queries

You can create dynamic queries by using ColdFusion variables within your SQL statements.

 

Example:

<cfquery name=öMyQueryö datasource=ödsö>

SELECT * FROM Employees

WHERE Emp_ID = #val(form.EmpID)#

</cfquery>

 

This query will retrieve employee information for the employee whoÆs id matches form.EmpID. The val() function ensures that form.EmpID is a numeric datatype.


⌐ Australian Consolidated Press 2001. All rights reserved.