Skip to main content
Known Participant
November 7, 2006
Question

Coldfusion loop in sql query

  • November 7, 2006
  • 2 replies
  • 803 views
Hi This is my problem

I am trying to build a cart system for a website and I was going to store the information in a cookie then pull the cookie elements loop through and build the sql query based on my output.

Now the output I get when I run it looks fine but the query return nothing, but when I grab the output and put it in the query litteraly through EM it return the values? My question is, will this work? Also if not what should I be useing? I have seen useing the CLIENT vars might be a better way? Anyone have any tutorials? Or are sessions good for this? I read they were not good for carts if the CF install was on a cluster.

Thanks for all your help.
Mike.


This topic has been closed for replies.

2 replies

Inspiring
November 8, 2006
Try using the IN() function. First create a comma-delimited, quoted list of the stock numbers using your CFLOOP code.

<cfloop list="#Cookie.MDCCart#" index="i">
<cfset StockNumberList = StockNumberList &"'"&i&"',">
</Cfloop>
<cfset StockNumberList=mid(StockNumberList,1,len(StockNumberList)-1)>

<Cfquery....>
SELECT * FROM Mytable
WHERE StockNo IN(#StockNumberList#)
</cfquery>
Known Participant
November 7, 2006
i just want to understand what you are trying to do.

Are you trying to loop through different column names?
proximoAuthor
Known Participant
November 7, 2006
Well kind of, what I want to do is have people be able to add products to there cart via a cookie, basicaly just add in the stock number then loop through them and have the loop through make a sql statement like

SELECT *
WHERE (StockNo = this OR StockNo= This OR StockNo= this)

so it would pull only the items in the cart and I could also pull other information like some details etc.