Skip to main content
Inspiring
September 28, 2012
Question

Help with QueryNew

  • September 28, 2012
  • 3 replies
  • 951 views

Hi,

I have this code:

<cfset mySites = QueryNew("ID, Nombre", "CF_SQL_VARCHAR, CF_SQL_VARCHAR")>

<cfloop from="1" to="3" index="x">

    <cfset QueryAddRow(mySites, 1)>

    <cfset QuerySetCell(mySites, "ID", "myID" & x , x)>

    <cfset QuerySetCell(mySites, "Nombre", "Name 001" , x)>

</cfloop>

<cfloop from="1" to="6" index="x">

    <cfset QueryAddRow(mySites, 1)>

    <cfset QuerySetCell(mySites, "ID", "myID" & x , x)>

    <cfset QuerySetCell(mySites, "Nombre", "Name 002" , x)>

</cfloop>

<cfquery name="getResult" dbtype="query">

    select * from mySites

    order by ID

</cfquery>

<cfdump var="#getResult#" />

I want to put the result of two loops into one query. This is the result:

query
RESULTSET
query
IDNOMBRE
1[empty string] [empty string]
2[empty string] [empty string]
3[empty string] [empty string]
4myID1 Name 002
5myID2 Name 002
6myID3 Name 002
7myID4 Name 002
8myID5 Name 002
9myID6 Name 002
CACHEDfalse
EXECUTIONTIME2
SQLselect * from mySites order by ID

Why the first 3 rows are empty?

Am I missing something?

Thanks for your help!

Best,

    This topic has been closed for replies.

    3 replies

    Inspiring
    September 29, 2012

    Yes, the thing you are missing is that you're not making any attempt to debug this yourself (sorry to be blunt about it).

    If you had checked what your query contained at various steps of your process, you'd've seen where your logic error is fairly quickly.

    I recommend you put a CFDUMP of your recordset at the bottom of the second loop, and pay attention to its output.  That should quickly reveal where you're going wrong.

    This sort of process (debugging / troubleshooting) should always be your first step in problem-solving, before resorting to asking other people for help.  Not that there's anything wrong with asking for help - that's what we're here for - but you should make a reasonable effort to help yourself first.

    Lemme know if doing some debugging of your own first doesn't reveal the problem.  But you need to focus on that second loop.  That's where your code is wrong.

    --

    Adam

    Participating Frequently
    September 28, 2012

    Whups –

    My mistake – just move your index variable in with the content. Spoke too quickly!

    <cfdump var="#getResult#" /

    jfb00Author
    Inspiring
    September 28, 2012

    Thanks for your reply and help.

    I don't understand, can you show me an example?

    I use variable "y" in my second query and the same issue.

    Participating Frequently
    September 28, 2012

    I personally don’t add any additional parameters when using QueryNew, etc. This works quite well. I’ve also changed your second index to ‘y’ out of no reason at all.

    Dave

    <cfdump var="#getResult#" /