Skip to main content
Known Participant
June 17, 2011
Question

Query Results give Empty Output?

  • June 17, 2011
  • 3 replies
  • 3252 views

Hello,

I am trying to create a text file from a coldfusion output table. The followng code is returning an empty set, when in fact I know there are records returned. Any insight would be greatly appreciated!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>

</head>

<body>

<CFQUERY name="by_tr" datasource="surveyalpha" dbtype="odbc">

select *
   
FROM sire.dbo.control_doc 

WHERE sire.dbo.control_doc.publish = 'yes' and
       sire.dbo.control_doc.township_range like '%#form.text#%'
   
 
<CFIF Not IsDefined("#form.field#")>
                ORDER BY section
<cfelseif form.field is "section">
    ORDER BY section  
<cfelseif form.field is "grid_address">
    ORDER BY grid_address
<cfelseif form.field is "point_name">
    ORDER BY point_name
</cfif>

</CFQUERY>

<cfif by_tr.recordcount greater than 0>

<pre>
Point Name|Northing|Easting|Differential Elevation|RTK Elevation|Monument Type|Monument Description|Grid Address|Township & Range|Section|Latitude|Longitude|Monument Notes
</pre>

<cfoutput query="by_tr">
<pre>
#Point_Name#|#Northing#|#Easting#|#Elevation_Diff#|#elevation_rtk#|#Mon_type#|#Mon_description#|#Grid_Address#|#Township_Range#|#Section#|#Latitude_DMS#|#Longitude_DMS#|#Mon_notes#
</pre>
</cfoutput>

<cfelse>
<pre>
Your search yielded no results.
</pre>
</cfif>

</body>
</html>

    This topic has been closed for replies.

    3 replies

    Inspiring
    June 17, 2011

    I am trying to create a text file from a coldfusion output table. The followng code is returning an empty set, when in fact I know there are records returned.

    What make you say this?  IE: what process did you go through to determine that records have been returned to CF?  I don't know of a situation in which a recordset is returned to CF and when looping over it yields unexpected results (ie: none).

    I more suspect your query doesn't return any records.  I know some of the other respondent have leapt forward from here, but let's go back to basics. How did you establish your CFQUERY returned records?  Straight after your CFQUERY, do a CFDUMP of your record set. Does it output a recordset?  I suspect not.

    So... why not?  Your CFDUMP will also output the SQL that was passed to the DB driver.  Does that SQL seem right?  If it doesn't, assess why it doesn't.  You've spotted that there's an unexpected value in your filter statement: 1N3W,View Text.  You almost certainly don't want that.  But how did "1N3W,View Text" get into form.text?  DOing a CFDUMP of the FORM scope will confirm its value.  But - assuming you wanted one or the other of those comma-delimited values, why are there two values there?  A quick google through the docs about how forms work yields this page: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-6eb7.html#WSc3ff6d0ea77859461172e0811cbec0f76c-7fed, and down the bottom it says this:

    {quote}

    If multiple controls have the same name, one form variable is passed to the action page with a comma-delimited list of values.

    {quote}

    So that's likely to be the cause of that then.  Time to check your form.

    [etc]

    I'm repeating a lot of stuff other people have said here, but hopefully it's a more cohesive example of how to troubleshoot your problems.

    Tips:

    * don't start with asserts like "it should be doing x".  Start with conceding "it's not doing x, so why not?"

    * RTFM.  I've been doing CF for over ten years now, and a day would not go by that something I don't expect happens, and my first port of call is to RTFM.  Even when answering questions here, I RTFM before I make a statement,  I've done three searches through the docs in the course of writing this reply, because I wasn't sure of what my expectations were.  So I checked them.

    * work through a problem in small steps.  Identify something unexpected, and then work out why that unexepcted thing happened to be.  Don't just go "humph... why did this not work?"

    From a code perspective:

    * read up on MVC.  You should never have a CFQUERY tag in a template which also has HTML in it.

    * read up on CFQUERYPARAM.  Do not hard-code dynamic values in your SQL string.

    * don't do SELECT * unless you actually will be using every single column.  This is very seldom the case.

    * do not specify the dbtype attribute of CFQUERY.  It's deprecated.  CF doesn't use ODBC any more, so suggesting that it does / ought to is just plain wrong (unless, of course, you're using CF5 or earlier, I mean.  I suspect not).

    * structKeyExists() is more efficient than isDefined()

    * learn when and when not to use #-signs.  Your isDefined() is almost certainly wrong.

    --

    Adam

    Known Participant
    June 17, 2011

    Wow, Adam thanks for the information.

    The empty output is from code that is making a text file from a records table, so if the records table has values/records, the text file should as well.

    With help from this forum, I realized I had name="text" in two places in my code, therefore there were no records returned for the text file I was trying to create, even though there were some for the records table.

    Your advice is helpful. Thanks!!

    WolfShade
    Braniac
    June 17, 2011

    <cfif by_tr.recordcount greater than 0>

      should be

    <cfif by_tr.recordcount gt 0>

    And, yes, as long as it's not used within a loop, CFDUMP can be your best friend when troubleshooting.

    CFDUMP inside a loop can immediately become your worst enemy, as a developer.

    ^_^

    Inspiring
    June 17, 2011

    <cfif by_tr.recordcount greater than 0>

      should be

    <cfif by_tr.recordcount gt 0>

    They are the same; read the docs:

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09d55-7ffc.html#WSc3ff6d0ea77859461172e0811cbec09d55-7ff9

    --

    Adam

    Inspiring
    June 17, 2011

    Hard to tell without seeing the data that is in the DB but I would start by remving some of the condition in the query and then add them back in one at a time.

    Run this and see if you get any results:

    <CFQUERY name="by_tr" datasource="surveyalpha" dbtype="odbc">

    select *
    FROM sire.dbo.control_doc 
    </CFQUERY>

    If you do, the run this:

    <CFQUERY name="by_tr" datasource="surveyalpha" dbtype="odbc">

    select *
    FROM sire.dbo.control_doc

    WHERE sire.dbo.control_doc.publish = 'yes'
    </CFQUERY>

    Keep adding conditions to the query until it stops returning values. You should then know why nothing is being returned.

    Use <cfdump var="#by_tr#"> to see the results.

    Known Participant
    June 17, 2011

    Thanks! I did that and it appears that the line:

    sire.dbo.control_doc.township_range like '%#form.text#%'

    is when I start getting an empty output. It obviously doesn't like '%#form.text#%'.

    Any advice as to why?

    Inspiring
    June 17, 2011

    What is the value of form.text? I am not seeing a form in the code you posted, is there a form?

    At the bottom of the  cfdump it should display the query it is running. Make sure that the CF  code, "#form.text#', is being replaced with the actual value.