Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." 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 Basics 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
  #1 (permalink)  
Old August 15th, 2004, 08:15 PM
SoC SoC is offline
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default Database search - more efficient way?

Currently have a search form which allows you to search a simple database.

The search form has a series of checkboxes and a textfield.

The problem is that if someone does not enter anything in the textfield, the SQL looks for a record in the database containing nothing. This means that no results will be returned.

I can get around this by using an IF then ELSE statement which says if the length of the text field is 0 then search on the checkboxes only, ELSE search on the checkboxes and the textfield. The problem is that this ends up being a relatively large file size.

I was just wondering if there is a way of saying IF the LENGTH of the textfield = 0 then LET its VALUE = 'a'. Because every single record in the database contains the letter 'a', I know that it will search all records.

I guess there is another, better way of doing this though. Any help much appreciated.

SoC

  #2 (permalink)  
Old August 15th, 2004, 08:34 PM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

SoC,

One alternative to this would be to only include the where clause if there is something in field.

Code:
SQL = "Select * from mytable where 1=1 "
if len(textfield) > 0 then SQL = SQL & " and dbFieldvalue = '"  & textfield & "'"
if len(textfield2) > 0 then SQL = SQL & " and dbFieldvalue2 = '"  & textfield2 & "'"
Another alternative is to use the LIKE statement.
This is quite common with search fields because it allows the user to put in part of the text being searched for.
Code:
SQL = "Select * from mytable where" 
SQL = SQL & " dbFieldvalue LIKE '%" & textfield & "%'"
SQL = SQL & " and dbFieldvalue2 LIKE '%" & textfield2 & "%'"
The advangage of the second method is that you can enter part of the text and still return a result. So if you are seaching for 'john', records containing 'john' would be returned as well as records containing 'johnson'.
So if the textfield was empty it would return all rows.
The disadvantage is that it is a slower query.


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
  #3 (permalink)  
Old August 15th, 2004, 08:49 PM
SoC SoC is offline
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rodmcleay,

thanks for your help.

Sorry, I should have specified in my original post that I
have been using the LIKE statement in my search (LIKE %textfield%) but for some reason it does not return any results.

Any thoughts?

SoC

  #4 (permalink)  
Old August 15th, 2004, 08:52 PM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Can you post your code for the statement?


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
  #5 (permalink)  
Old August 15th, 2004, 08:56 PM
SoC SoC is offline
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I think I just solved it.

I'm using Dreamweaver, which lets you set the default value for the
form variable.

Up until now I had this set to 'null'.

All I needed to do was change this to nothing! Aaagh!

Cheers

SoC



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search in Database????? Samora C# 2 October 10th, 2007 07:41 AM
need help, to Search from database vikky17 Classic ASP Basics 1 February 5th, 2006 12:35 AM
database search rajuru PHP Databases 2 November 9th, 2004 04:32 PM
Search Database bspradeep Classic ASP Databases 1 September 15th, 2004 05:47 AM





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