- ColdFusion 9 Developer Tutorial
- John Farrar
- 410字
- 2021-08-05 16:16:37
Returning data from the CFC
Lastly we return the rsReturn
variable to the caller. You will note that this has changed the variable type to a ColdFusion query which is the name for the returned recordset from<cfquery>
.
Now we need to create a calling page. We have to see what is coming back from the database and how it looks inside ColdFusion. Here is the code to our calling page.
<!--- Example: 2_4.cfm ---> <!--- Processing ---> <cfscript> objProduct = createObject("component","product_2").init(); rsProducts = objProduct.getRecordset(); </cfscript> <!--- Content ---> <cfdump var="#rsProducts#">

We see something new in the recordset dump since ColdFusion version 8. This version includes the attributes for cached, execution time, and the SQL that was run to produce the query. These can be very helpful for development, debugging, and logging. You will also see that the result set displays each row in a table. Each of the fields returned from the database are columns in the recordset. The number to the left of the table is the row of the recordset returned; it is not the index of the database. That is something you should note. Good database tables should always have a primary index key to refer to the individual record at a later time. You will find we use a field called ID in this database table. Yes, we only have five records in our database, if you are wondering. This is to keep things as simple as possible.
Now we need to be able to use this recordset in more places than<cfdump>
. Let's take a look at how this is done. We will use a number of examples to show you how to retrieve data back from your recordset. In the first example, we will use<COUTPUT>
and present the results in an unordered list. We simply add an attribute called query
and set it equal to the recordset we retrieved from our object instance. Here is the code for this example:
<!--- Example: 2_5.cfm ---> <!--- Processing ---> <cfscript> objProduct = createObject("component","product_2").init(); rsProducts = objProduct.getRecordset(); </cfscript> <!--- Content ---> <ul><cfoutput query="rsProducts"> <li>#rsProducts.name#</li></cfoutput> </ul>

Now we are going to modify the code so it gets linked back and we can return an individual record. We will put them on the same page, but how you do this may change depending on what you are doing. This is just an example. Let's look at the modified code. We will just change the code from example 2_5
this time. Add or modify the highlighted rows to the code:
<!--- Example: 2_5.cfm ---> <!--- Processing ---> <cfparam name="url.id" default=""> <cfscript> objProduct = createObject("component","product_2").init(); rsProducts = objProduct.getRecordset(); rsProduct = objProduct.getRecordset(where = "id = #url.id#"); </cfscript> <!--- Content ---> <ul><cfoutput query="rsProducts"> <li> <a href="?id=#rsProducts.id#">#rsProducts.name#</a> </li></cfoutput> </ul> <cfif rsProduct.recordCount EQ 1> <cfoutput><table> <tr> <th>Product</th> <td>#rsProduct.name#</td> </tr> <tr> <th>Description</th> <td>#rsProduct.description#</td> </tr> <tr> <th>Price</th> <td>#dollarFormat(rsProduct.price)#</td> </tr> </table></cfoutput> </cfif>
- UG NX 12.0中文版完全自學一本通
- 魔法詞典:AI繪畫關鍵詞圖鑒(Stable Diffusion版)
- 超簡單的攝影后期書
- 零基礎學后期:Photoshop+Lightroom數碼照片處理從新手到高手
- Web 2.0 Solutions with Oracle WebCenter 11g
- BIRT 2.6 Data Analysis and Reporting
- After Effects CC 2018影視特效與合成案例教程
- 中文版Photoshop CS6應用技法教程
- CorelDRAW X6核心應用案例教程(全彩慕課版)
- Service Oriented Architecture with Java
- 精通AutoCAD 2013機械設計
- The 3CX IP PBX Tutorial
- Alias 2013工業設計完全自學一本通
- Photoshop CS5平面設計教程
- Photoshop CC實例教程(第4版)