Adsense

Monday, August 22, 2011

Coldfusion - assign value of a database query to a variable using CFSET and CFQUERY


I know its rare that anyone uses ColdFusion nowadays, but today, I had to add in a little "feature" to an in-house web application in my company, which was developed using ColdFusion.  Looking at the timestamp of the files, the application was built way back in 2001. Woah, that's like 10 years ago! @.@

Whats worse is that, I have never used ColdFusion to develop full web applications before! And we all know how difficult it is to modify OTHER PEOPLE'S codes.  So I had to do a bit of guesswork and lots of Googling before figuring out how to do a VERY SIMPLE thing: how to assign the value of a database query to a variable.

It was fun seeing the thing work though I must confess, I am still a fan of ASP.NET ;)

<cfquery name="getcompanyinfo" datasource="mydatabasename" dbtype="ODBC" >
select c.companyid, c.address, c.tel from companies c where c.comp_name like '%#companyname#%'</cfquery>

<CFSET COMPANYID = getcompanyinfo.companyid>

<cfquery name="getprojdetails" datasource="mydatabasename" dbtype="ODBC">
select p.id,p.projname, p.projdetails from project p where p.companyid = '#COMPANYID#'
</cfquery>

<cfoutput query="getcompanyinfo">
#companyid#<br>#address#<br>#tel#
</cfoutput>
<hr>
<cfoutput query="getprojdetails">
#id#<br>#projname#<br>#projdetails
</cfoutput>

Pin It

No comments:

Post a Comment