网页的搜索功能如何实现?

2024-12-15 09:44:58
推荐回答(4个)
回答1:

把文本框的值传进来,然后利用SQL语句中的like模糊查询。
如 sql="select * from news where title like '%"&request("title")&"%'"
楼主在查询的时候还要注意过滤空格和单引号等。
下面这文章是介绍ASP做查询功能,楼主可以看一下。
详见:
面是库中URLINDEX表:URL和Keywords字段分别添加了索引.
URL 文本 (索引:有(无重复))
Title 文本
Description 文本
Summary 文本
Keywords 文本(索引:有(无重复))

简单搜索引擎
LEFTMARGIN=0 TOPMARGIN=0>


Query:





<%
dim act
act=request("act")
if(act="search") then
QueryString = rm( "QueryString" )
QueryWords = Split( QueryString )
strIndent = " "

' 如果搜索为空则返回
If QueryString = "" Then
Response.Redirect( " p" )
End If

Session.timeout = 2
If IsObject(Session("sitesearch_conn")) Then
Set conn = Session("sitesearch_conn")
Else
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("database/ b"),"",""
Set Session("sitesearch_conn") = conn
End If
' 查询语句
sql = "SELECT * FROM [URLIndex] WHERE"

'搜索Description字段
sql = sql & " ( [Description] LIKE '%" & QueryWords( 0 ) & "%'" ' First
For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
If uCase( QueryWords( i-1 ) ) = "OR" Then
sql = sql & " OR [Description] LIKE '%" & QueryWords( i ) & "%'"
Else
sql = sql & " AND [Description] LIKE '%" & QueryWords( i ) & "%'"
End If
End If
Next

' 搜索Keywords字段
sql = sql & " ) OR ( [Keywords] LIKE '%" & QueryWords( 0 ) & "%'"
For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
If uCase( QueryWords( i-1 ) ) = "OR" Then
sql = sql & " OR [Keywords] LIKE '%" & QueryWords( i ) & "%'"
Else
sql = sql & " AND [Keywords] LIKE '%" & QueryWords( i ) & "%'"
End If
End If
Next

' 搜索Title字段
sql = sql & " ) OR ( [Title] LIKE '%" & QueryWords( 0 ) & "%'"
For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
If uCase( QueryWords( i-1 ) ) = "OR" Then
sql = sql & " OR [Title] LIKE '%" & QueryWords( i ) & "%'"
Else
sql = sql & " AND [Title] LIKE '%" & QueryWords( i ) & "%'"
End If
End If
Next

' 搜索Summary字段
sql = sql & " ) OR ( [Summary] LIKE '%" & QueryWords( 0 ) & "%'"
For i = LBound( QueryWords ) + 1 to UBound( QueryWords )
If QueryWords( i ) <> "" and UCase( QueryWords(i) ) <> "OR" and UCase( QueryWords(i) ) <> "AND" Then
If uCase( QueryWords( i-1 ) ) = "OR" Then
sql = sql & " OR [Summary] LIKE '%" & QueryWords( i ) & "%'"
Else
sql = sql & " AND [Summary] LIKE '%" & QueryWords( i ) & "%'"
End If
End If
Next
sql = sql & " )"

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 3, 3
Response.Write "
你搜索的是: " & QueryString
Response.Write "
搜索的关键字: "
For i = LBound( QueryWords ) to UBound( QueryWords )
Response.Write "
" & strIndent & i & ": " & QueryWords( i )
Next

' Print the SQL String
Response.Write "
sql 语句 : " & sql

' Print the Results
Response.Write "
结果 :
    "
    On Error Resume Next
    rs.MoveFirst
    Do While Not rs.eof
    Response.Write "
    " & "" & rs.Fields("Title") & " - "
    Response.Write rs.Fields("Description") & "
    "
    Response.Write " URL: " & rs.Fields("URL") & ""
    Response.Write "
    "
    rs.MoveNext
    Loop
    Response.Write "
"
end if
%>


回答2:

需要数据库搜索,形式为:
select * from 书目录表 where 书名(内容) like '%关键字%'
可以检索出包含关键字的书。

回答3:

兄台...你会asp的select update delete么?分的话很简单的.
搜索主要用到sql语句完成的!
找一下资料自己研究..找别人帮你做.自己也学不到东西.懂?
如果遇到什么问题请加52433292群.共同研究!

回答4: