Solved

Searching an SQL Database

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Adobe Dreamweaver CS5 is a WYSIWYG web page editor that has advanced HTML, CSS, and Javascript rendering functionality and is probably the most well-known HTML editor available. Much of Dreamweaver's appeal centers around the Design View interfac…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

20 Experts available now in Live!

Get 1:1 Help Now