Solved

Searching an SQL Database

Posted on 2004-09-23
2
159 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For those who don't know, Adobe Dreamweaver is a popular commercial web editor that enables you to design, build and manage complex websites. The editor is a WYSIWYG (What You See Is What You Get) web editor, which means that you can create your web…
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now