Skip to main content
Participant
December 22, 2009
Answered

Returning querys with no results, help?

  • December 22, 2009
  • 3 replies
  • 621 views

Hi I am trying to query my database to show what posts an individual user has created, which does work, but if a user has submitted no posts then I cannot get my CFIF statement to display some text, I think the codes right I just cannot figure out why it wont work, any help appreciated.

Heres the code:

<cfquery name="q2" datasource="datasource">

select * from blogentries where blogauthor = '#q1.user_firstname# #q1.user_surname#' order by [ID] desc

</cfquery>
<img src="images/yourposts.jpg" /><br />
<h2><u>News items written by you.</u></h2>
<cfoutput query="q2">
<cfif q2.RecordCount is '0'>
You have no posts at the current moment in time.
<cfelse>

<a href="blogs.cfm?id=#id#"><h2>#blogtitle#</h2></a><span class="orangebit">#submissiondate#</span>
<p>#left(blogdescription,200)#</p>
<hr />
</cfif>

Anyone know where i'm going wrong?

Thanks.

    This topic has been closed for replies.
    Correct answer joecopley

    The previous reply seemed to have the answer but the message looks truncated.

    To repeat, if there are no results, the query loop doesn't execute. So your test of the record count and the not-found message never runs. Put it outside of the query loop.

    Also I don't see the closing cfquery tag, which should throw an error, but I assume you just didn't paste it into your message.

    Joe

    3 replies

    Participant
    December 22, 2009

    Thanks folks, problem solved.

    joecopleyCorrect answer
    Inspiring
    December 22, 2009

    The previous reply seemed to have the answer but the message looks truncated.

    To repeat, if there are no results, the query loop doesn't execute. So your test of the record count and the not-found message never runs. Put it outside of the query loop.

    Also I don't see the closing cfquery tag, which should throw an error, but I assume you just didn't paste it into your message.

    Joe

    Inspiring
    December 22, 2009

    Fixed.  Darn forum software ;-)

    Inspiring
    December 22, 2009

    Since there are no results in the query, any code inside the  <cfoutput> never executes.  The cfif needs to be moved outside the  cfloop.

    <cfif q2.RecordCount eq 0>


         no results
    <cfelse>
         <cfoutput query="q2">
          ...output the results
         </cfoutput>
    </cfif>

    Though the cfelse is not  strictly necessarily.