Solved

Searching an SQL Database

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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