Tutorial: Search a Database

Reply

Join Date: Apr 2004
Posts: 167
Reputation: Drew is an unknown quantity at this point 
Solved Threads: 7
Drew's Avatar
Drew Drew is offline Offline
Junior Poster

Tutorial: Search a Database

 
0
  #1
Apr 5th, 2005
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".

  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.

  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.

  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.

  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.

  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.

  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!

  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
Drew Gauderman
ASP / MSSQL Coder
http://www.iportalx.net - My ASP Portal
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 18
Reputation: millers_35 is an unknown quantity at this point 
Solved Threads: 2
millers_35 millers_35 is offline Offline
Newbie Poster

Re: Tutorial: Search a Database

 
0
  #2
Apr 6th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 167
Reputation: Drew is an unknown quantity at this point 
Solved Threads: 7
Drew's Avatar
Drew Drew is offline Offline
Junior Poster

Re: Tutorial: Search a Database

 
0
  #3
Apr 7th, 2005
This example was for an access database. For MSSQL you just have to change the connection string and it should work
Drew Gauderman
ASP / MSSQL Coder
http://www.iportalx.net - My ASP Portal
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 131
Reputation: william_stam is an unknown quantity at this point 
Solved Threads: 2
william_stam's Avatar
william_stam william_stam is offline Offline
Junior Poster

Re: Tutorial: Search a Database

 
0
  #4
May 8th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC