944,208 Members | Top Members by Rank

Ad:
  • ASP Discussion Thread
  • Unsolved
  • Views: 9120
  • ASP RSS
Apr 5th, 2005
0

Tutorial: Search a Database

Expand Post »
How to search a database

Intro

Searching a database is actually a easy thing. It just involves a form, and a page that displays the results. Im going to show you how to make a simple form, and a page that displays the results.

First steps

So the first step is to create a form from where we search. This is just some simple html. Save this as page "search.asp".

ASP Syntax (Toggle Plain Text)
  1. <table>
  2. <tr>
  3. <form method="POST" action="search_results.asp">
  4. <td>Search: <input type="text" name="txtSearch" size="30"></td>
  5. </tr>
  6. <tr>
  7. <td><input type="submit" value="Search"></td>
  8. </tr>
  9. </table>

Next we are going to make the search page. This will be the page where the database is going to be searched. We need to open the database, create a SQL string to search the database, and display the results if there are any.

Define variables

First lets define all the variables we are going to use.

ASP Syntax (Toggle Plain Text)
  1. <%
  2. Dim strInputSearch 'Variable for the search word
  3. Dim strCon 'Holds the string to connect to the db
  4. Dim adoCon 'Database Connection Variable Object
  5. Dim strSQL 'Holds the SQL query for the database
  6. Dim rsSearch 'Holds the search recordset


Search field

Next lets get the search field and make it so no one can inject any bad SQL code.

ASP Syntax (Toggle Plain Text)
  1. 'This is the variable that has the search word
  2. strInputSearch = Request.Form("txtSearch")
  3.  
  4. 'This makes it so people cant inject SQL code and/or cause some unwanted errors
  5. strInputSearch = Replace(strInputSearch,"'", "''", 1, -1, 1)

Database connection

Next we are going to create a database connection before we can search the database. If your database is a different name, you need to change the name in the red.

ASP Syntax (Toggle Plain Text)
  1. 'This sets the connection
  2. Set adoCon = Server.CreateObject("ADODB.Connection")
  3.  
  4. 'This is the connection string
  5. strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &Server.MapPath("database.mdb")
  6.  
  7. 'Open the connection
  8. adoCon.Open strCon

Open and search

Now we are going to open the database and search it.

ASP Syntax (Toggle Plain Text)
  1. 'Set the database connection
  2. Set rsSearch = Server.CreateObject("ADODB.Recordset")
  3.  
  4. 'This is the SQL statement for searching. You will need to change tblTable and Field to match your database (they are in red)
  5. strSQL = "SELECT tblTable.* FROM tblTable WHERE tblTable.Field LIKE '%" & strInputSearch & "%';"
  6.  
  7. 'Opens the database so we can get the results of the search
  8. rsSearch.Open strSQL, adoCon

Display results

Now we are going to display if there are any results, or if there are none.

ASP Syntax (Toggle Plain Text)
  1. 'If there are no matches then continue
  2. If rsSearch.EOF Then
  3.  
  4. 'Write out the message that there are no matches
  5. Response.Write("There are no matches with your search")
  6.  
  7. 'If there are matches, continue
  8. Else
  9.  
  10. 'Loop through the database to display all the matches
  11. DO UNTIL rsSearch.EOF
  12.  
  13. 'Write out the match found. You need to change Field to your database field you are search (its in red)
  14. Response.Write(rsSearch("Field") & "<br>")
  15.  
  16. 'Move to next line in database
  17. rsSearch.MoveNext
  18. 'Continue looping through database to display all results found
  19. Loop
  20. End If


Now that you opened the database and returned the results, it is very important to close all the connections and we are all done!

ASP Syntax (Toggle Plain Text)
  1. 'Reset server objects
  2. Set rsSearch = Nothing
  3. adoCon.Close
  4. Set adoCon = Nothing
  5. %>
Last edited by happygeek; Oct 28th, 2006 at 10:37 am. Reason: Formatting
Similar Threads
Reputation Points: 25
Solved Threads: 7
Junior Poster
Drew is offline Offline
166 posts
since Apr 2004
Apr 6th, 2005
0

Re: Tutorial: Search a Database

Is this using an SQL database? And I didnt see the files you said were listed to see the flow.
Thanks! Thats good stuff there.

Miller
Reputation Points: 11
Solved Threads: 2
Newbie Poster
millers_35 is offline Offline
18 posts
since Apr 2005
Apr 7th, 2005
0

Re: Tutorial: Search a Database

This example was for an access database. For MSSQL you just have to change the connection string and it should work
Reputation Points: 25
Solved Threads: 7
Junior Poster
Drew is offline Offline
166 posts
since Apr 2004
May 8th, 2005
0

Re: Tutorial: Search a Database

how do you get a multiword search going. like google.

the above example if you search for "and the" it would only return the records where those two words are together. how do you get it so that it returns records that contain both words but not necessarly together?

thanks, i like the strip sql part, i have been using client side till now.
Reputation Points: 10
Solved Threads: 2
Junior Poster
william_stam is offline Offline
131 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP Forum Timeline: Please Help With Code
Next Thread in ASP Forum Timeline: Error publishing Asp pages on WinME, personal webserver





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC