ASP單表單字段多關(guān)鍵字查詢

2010-08-28 10:52:19來源:西部e網(wǎng)作者:

<%
Function SearshSQL(Tname,Lname,Str) 'Tname-表名 Lname-列名 Str-查詢的字符
 If Trim(Str)="" then
 MySql="Select * From "&Tname
 Else 
 Str=SqlEncode(Str)    '預(yù)處理查詢字符串
 ArrStr=split(Str," ")  '用空格分割處理后字符串
 Umax=Ubound(ArrStr)   '分割后數(shù)組上維,確定分割成關(guān)鍵詞的個(gè)數(shù)
 If Umax>0 Then    '關(guān)鍵詞中多個(gè)空格的處理,從第2維開始處理
  For i=1 to Umax
   StrTemp=ArrStr(i)
   If Trim(StrTemp)="" Then StrTemp="/~~~~~/" '分割成的元素為空用特殊字符代替
   Sql=Sql &" Or "& Lname &" like '%"& StrTemp &"%'"
  Next
 End If
 
 MySql="Select * From "& Tname &" where "& Lname &" like '%"&ArrStr(0)&"%'"&Sql
 
 Do
   j=InStr(1,MySql, "%/~~~~~/%",1)
   If j=0 Then
  Exit Do
   End If
   MySql=Replace(MySql," Or "&Lname&" like '%/~~~~~/%'","")
 Loop
 End IF 
 
 
SearshSQL=MySql

end function

Function SqlEncode(Str)  '格式化字符串
str=replace(str," "," ") '先把左右的全角空格替換成半角空格
str=trim(str)
str=replace(str,"["," ")
str=replace(str,"';"," ")
str=replace(str,"'"," ")
str=replace(str,"_"," ")
str=replace(str,"%"," ")
str=replace(str,"+"," ")
str=replace(str," "," ")
sqlencode=str
end function
%>

測(cè)試
<%=SearshSQL("user","name","我們 相信 關(guān)鍵詞  作弊 不是  SEO")%>

發(fā)表上去把單引號(hào)變?nèi)橇耍愿皆创a上來。
 
UploadFiles/2007-9/926184509.rar
關(guān)鍵詞:ASP

贊助商鏈接: