Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Searching an SQL Database

Posted on 2004-09-23
2
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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