比较不错的asp单表单字段多条件查询
程序员文章站
2022-03-25 21:39:03
用途:文章表内文章关键字查询 查询格式: 百度 google 百度好还是google好 百度+google (也可不输入条件全部查询) &...
用途:文章表内文章关键字查询
查询格式: 百度 google 百度好还是google好 百度+google (也可不输入条件全部查询)
<!--#include file="conn.asp"-->
<%
function searshsql(tname,lname,str) 'tname-表名 lname-列名 str-查询的字符
if trim(str)="" then
mysql="select * from "&tname
else
str=sqlencode(str) '预处理查询字符串
arrstr=split(str," ") '用空格分割处理后字符串
umax=ubound(arrstr) '分割后数组上维,确定分割成关键词的个数
if umax>0 then '关键词中多个空格的处理,从第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
%>
<%
sql=searshsql("article","coments","萝卜+白菜 狗熊") +"order by..(排序)"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if not rs.eof then
下面的自己写
查询格式: 百度 google 百度好还是google好 百度+google (也可不输入条件全部查询)
<!--#include file="conn.asp"-->
<%
function searshsql(tname,lname,str) 'tname-表名 lname-列名 str-查询的字符
if trim(str)="" then
mysql="select * from "&tname
else
str=sqlencode(str) '预处理查询字符串
arrstr=split(str," ") '用空格分割处理后字符串
umax=ubound(arrstr) '分割后数组上维,确定分割成关键词的个数
if umax>0 then '关键词中多个空格的处理,从第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
%>
<%
sql=searshsql("article","coments","萝卜+白菜 狗熊") +"order by..(排序)"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if not rs.eof then
下面的自己写
上一篇: JS基础语法---总结
下一篇: access中链接表的问题