Solved

Searching an SQL Database

Posted on 2004-09-23
2
161 Views
Last Modified: 2010-04-25
Hi,

I would like to include a search facility to one of my webpages which gives the user the chance to search on words of their choice.  The search should query one particular field within a table to see if there is a match for their chosen words.

For example I have a table which holds information about a particular new article.  The Fields are ref (reference number), newsheadline (News Headline), ndate (Date of Article) and newsarticle (News Article).

I would like the users words to search on the newsarticle (News Article) field and return any records where their chosen words were found in that field.

Any help would be very much appreciated.

Grant
0
Comment
Question by:grantballantyne
2 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 12139995
What's your server side language you use? generally, you need sql statement like:

select * from tblArticle where newsheadline like '%mykeyword%'
0
 
LVL 7

Accepted Solution

by:
jay_eire earned 500 total points
ID: 12142662
If you are using ASP try this.....

STEP 1
Create Your Form...this is the easy part...
the form is where your users enter their search criteria

<!-- *************** FORM CODE START ***************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Search Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="NewsSearch" method="post" action="site_search_results.asp">
  <input name="searchbox" type="text" id="searchbox">
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

*************** FORM CODE END *************** -->


STEP 2
The Form Posts itself to you Search Results Page........
which will build a mini table for each of the news article that matches the search

criteria entered by the user, if the search cant find anything it displays a

messege to the user Sorry No Results Found! Please Try Again ......

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title>News Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="660" border="0">
      <tr>
        <td width="98%"><div align="left">
            <br>
        <!--- ASP SEARCH CODE BEGIN -->
            <!-- ######################### -->
            <!-- Connect To Your Database -->
<%
Dim ConnectNow
ConnectNow = "DRIVER={SQL

Server};SERVER=servername;UID=sa;PWD=letmeinplease;DATABASE=mydatabase"%>
<%
Dim SearchFor
SearchFor = "1"
If (Request.Form("searchbox") <> "") Then
  SearchFor = Request.Form("searchbox")
End If
%>
<!-- Get the news articles the user has search for -->
<%
Dim Recordset
Dim Recordset_numRows

Set Recordset = Server.CreateObject("ADODB.Recordset")
Recordset.ActiveConnection = ConnectNow
Recordset.Source = "SELECT * FROM dbo.tblnews WHERE newsarticle LIKE '%" +

Replace(SearchFor, "'", "''") + "%' ORDER BY ndate ASC"
Recordset.CursorType = 0
Recordset.CursorLocation = 2
Recordset.LockType = 1
Recordset.Open()

Recordset_numRows = 0
%>

</div>
<!---Tell The User What They Have Seached For -->       <div align="left">
<span >You Searched For: <strong><%= Request("searchbox") %></strong></span><br>
<br>
            <!-- Build a Table for the news article -->
                  <%
If Not Recordset.BOF Then
Recordset.MoveFirst

Do Until Recordset.EOF
Response.Write("<table width='400' border='1' align='center' cellpadding='0'

cellspacing='0' bordercolor='#666666'>" & Chr(13))
Response.Write("<tr>" & Chr(13))
Response.Write(" <td>&nbsp;" & Recordset.Fields("newsheadline") & "</td></font>" & 

Chr(13))
Response.Write("</tr>" & Chr(13))
Response.Write("<tr>" & Chr(13))
Response.Write(" <td>&nbsp;" & Recordset.Fields("newsarticle") & "</td></font>" & 

Chr(13))
Response.Write("</tr>" & Chr(13))
Response.Write("</table>" & Chr(13))
Response.Write("<br><br><br>" & Chr(13))
Recordset.MoveNext
Recordset.MoveNext
Loop

End If
%>           <br>
                   <!--If we cant find any results display this table -->
                   <% If Recordset.EOF And Recordset.BOF Then %>
             <table width="42%" height="20" border="2" align="center"

cellpadding="0" cellspacing="0" bordercolor="#666666">
               <tr>
                 <td ><div align="center"><font color="#FF0000"><strong>Sorry
                         No Results Found! Please Try

Again</strong></font></div></td>
               </tr>
        </table>
                   <%End if %>
             <br>
                    
          <!--Search End --></div><%
Recordset.Close()
Set Recordset = Nothing
%>
            <!-- ######################### -->
            <!--- ASP SEARCH CODE END -->
            <br>
            <br>
          </td>
      </tr>
    </table>

</body>
</html>



If you have lots and lots and lots and lots of news items in your database....
you could build a results page that would just display the new headline, date but

also with a link to a "DETAIL PAGE", this detail page would display the full news

item i.e News Headline, Date of Article, News Article

THanks
Jay Eire
(<>..<>)






0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I still run into .cgi files every now and then. In some instances, I actually prefer the simplicity of a .cgi script to other options. Since I use DreamWeaver extensively, what I needed was a way to open .cgi scripts in Dreamweaver. And I wanted to …
This article is very specific and is only intended to help if you are installing Dreamweaver 8 in a Windows 7 environment with Office 2007 installed.   I'm not sure why Microsoft tends to release OS' that should not be released but they do.  Windows…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question