ColdFusion Function to Delete Generic Records from Generic Tables

Thought I’d share a simple generic ColdFusion delete function I wrote this morning. We develop many sites where I work and often they each end up with their own set of unique content management tools. All of the sites share the need for CRUD (create, read, update, delete) functionality and often the query syntax is the same for data retrieval and deletion:

DELETE
FROM table
WHERE table_id = variable_id

In writing a component for some of the tools I started wondering if, in addition to data, I could also pass the names of database tables and columns to create an all-purpose generic delete function.
Continue reading “ColdFusion Function to Delete Generic Records from Generic Tables”

Output ColdFusion Query Results to XML

I don’t claim to be a ColdFusion expert by any stretch of the imagination, but recently I found myself in need of coming up with a way to output query results to an XML file. My example below is based on an absolutely hideous article I stumbled across on the ColdFusion Developer Center. The example they provide has several errors in syntax: unclosed tags, unclosed quotes, unOPENED pounds, etc. It also only outputs one record!

After about an hour of playing with it I was able to get the code working properly and I thought I’d share a very basic example. The following code queries a database for names and phone numbers then outputs them all to a file name phonelist.xml in the current path:

<cfquery name="qGetPhoneList" datasource="datasource">
select first_name,last_name,phone from employees
</cfquery>

<!—Create a temporary variable "tempxml" to hold our XML document—>

<cfset tempxml = "<?xml version =""1.0""?>
<!DOCTYPE phonelist [
<!ELEMENT phonelist (firstname,lastname,phone)>
]>
<phonelist>">
<cfloop query="qGetPhoneList">
<cfset tempxml = "#tempxml#
<cid>#Trim("#XMLFormat(first_name)#")#</cid>
<s>#Trim("#XMLFormat(last_name)#")#</s>
<r>#Trim("#XMLFormat(phone)#")#</r>
">
</cfloop>
<cfset tempxml = "
#tempxml#
</phonelist>">

<!—show the XML in the HTML output —>
<P>This is a simple XML document that’s been generated by the ColdFusion code. </P>
<cfoutput>
<xmp>
#tempxml#
</xmp>
</cfoutput>

<!—write out the XML—>
<cfset writePath=#ExpandPath("phonelist.xml")#>
<cfoutput>
#writePath#<br />
<cffile action = "Write" file="#writePath#" output="#tempxml#">
</cfoutput>

-rG