Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2006, 09:25 AM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP/MySQL Order by problem

Hello, I'm new to ASP/MySQL field so I have trouble with a simple 'order by' clause. Here is my code:

I want to sort recorset according to what was selected in this drop down:
----------------------
<%session("sorting") = Request.form("Sorting")
session("ID") = request.querystring("ID")%>
<form name="frmSort" action="Boutique_Products_Cat.asp" method="POST">
<div align="left">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%"><select size="1" name="Sorting" class="input-combo-field" onChange="document.frmSort.submit();" >
<option>--- Sort by</option>
<option value="ProdPrice">Price</option>
<option value="ProdType">Type</option>
<option value="CreateDate">Recent</option>
</select></td>
</tr></table></div></form>
----------------------
Here is my query:
<%
dim rs, sql
set rs=server.createobject("ADODB.recordset")
if session("sorting") <> "" then

sql = "select * from ProductDetails where CatID= " & session("ID") & " order by '" & session("sorting") & "'"
else
sql = "select * from ProductDetails where CatID= " & session("ID") & " order by CreateDate desc"

end if

set rs = conn.execute(sql) %>
----------------
I tried different options but every time it gives "syntax error, check your MySQL version for correct syntax", although it reads session variable correctly and sorts right when page first loads. I'm using MySQL 4.0.26 version. What am I doing wrong??

Thanks in advance
 
Old August 14th, 2006, 03:42 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

When your Session("sorting") != "" what does your sql statement equate to?

The reason the pages sorts correctly on the first page load is because you have your order by clause hard coded, e.g., your are ordering on CreateDate desc, my guess would be that either A) your session value to sort on contains a special characert (@, ', or , etc.) or B) the value of sorting is not contained in the SELECT list.

Post what your sql statements looks like once the session values have been populated.

"The one language all programmers understand is profanity."
 
Old August 14th, 2006, 03:55 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:B) the value of sorting is not contained in the SELECT list.
This is not required; you can easily sort on a column that you don't select.

The problem is with the apostrophes in the ORDER BY clause. Hard to spot because of the font, but look at this:

order by '" & session("sorting") & "'"

This results in something like:

order by 'ProdType'

which is incorrect SQL. Drop the apostrophes and things will work:

sql = "select * from ProductDetails where CatID= " & session("ID") & " order by " & session("sorting")

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old August 14th, 2006, 04:47 PM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

I tried this: sql = "select * from ProductDetails where CatID= " & session("ID") & " order by " & session("sorting") - it doesn't work either. Gives exactly the same error...

 
Old August 15th, 2006, 12:59 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Have you tried dparsons' tip? What do you get when you write out the SQL statement?

Response.Write("Sql is " & sql)
' set rs = conn.execute(sql)
%>

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old August 15th, 2006, 08:15 AM
Registered User
 
Join Date: Aug 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Problem solved, I'm ashamed to admit it but I never checked session("ID") value. Since it's getting value from querystring, it turns to null on form reload. I assigned it to session("ID1") and everything works fine now. Thank you for your prompt help!
 
Old August 15th, 2006, 12:52 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Glad you worked everything out.

"The one language all programmers understand is profanity."





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ORDER BY in SQL query in ASP page saifi4u Classic ASP Databases 1 February 29th, 2008 09:19 AM
MySQL DB insert ASP problem crmpicco Classic ASP Databases 4 March 10th, 2005 10:32 AM
give me a order about learning asp.net pipijin ASP.NET 2.0 Basics 0 January 2nd, 2005 10:23 PM
MySQL query desc order problem dssachdeva MySQL 3 September 18th, 2003 01:11 AM
ASP SQL fields in order ALoPresto Classic ASP Databases 2 July 25th, 2003 02:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.