我是新手。我搜索了Stackoverflow数周,试图找到高级搜索的代码。
最后创建了我自己的。它工作得很好。我将它列在这里供其他想要使用它的人使用。
的问题是:高级搜索mysql perl
- 当没有item_category选择我默认为
*
,但它不工作。 item_category是搜索唯一需要的变量吗?
我是否需要关注消毒或注入黑客?
完全匹配似乎没有工作。它可能会搜索100个单词作为'FREE',并且不会找到匹配。有任何想法吗?
关于可靠性和速度问题的任何建议?
$ ANDOR =参数( '及或');#创建一个AND OR搜索 如果($ ANDOR当量 “”){$ ANDOR = “AND”;}
如果($ item_category){ $ item_category =“$ item_category”; } else {$ item_category =“*”; } $ statement。=“item_category LIKE'$ item_category'”;
if($ item_state){ $ statement。=“$ ANDOR item_state LIKE'$ item_state'”; } if($ item_city){ $ statement。=“$ ANDOR item_city LIKE'$ item_city'”; ($ db_id){ $ statement。=“$ ANDOR db_id ='$ db_id'”; }
$ keywords = param('keywords');
如果($关键字) { 如果(参数( 'searchmatch')EQ “精确”) { $声明= “$ ANDOR ITEM_NAME = \” $关键字\ “OR ITEM_DESC = \” $关键字\ “ OR item_desc2 = \ ”$关键字\“ ”;#
} 其他 {$声明=“ $ ANDOR”; my @keywords = split(/ /,$ keywords);
foreach my $keyword(@keywords) { $statement .= " item_name LIKE '%$keyword%' OR item_desc LIKE '%$keyword%' OR item_desc2 LIKE '%$keyword%' "; }
} }
$ date_begin =参数( 'date_begin'); if($ date_begin){ $ statement。=“$ ANDOR modification_time>'$ date_begin'”; }
if($ user){ $ statement。=“$ ANDOR user LIKE'$ user'”; }
$ price_low = param('price_low'); $ price_high = param('price_high'); if((price_low)& &($ price_high)){ $ statement。=“$ ANDOR item_price BETWEEN'$ price_low'AND'$ price_high'”; ($(price_low)& &($ price_high eq“”)){ $ statement。=“$ ANDOR item_price BETWEEN'$ price_low'AND *”; ($ price_high)& &($ price_low eq“”)){ $ statement。=“$ ANDOR item_price BETWEEN'0'AND'$ price_high'”; } else { $ statement。=“”;
}
我而$ sth = $ dbh->准备(QQ(SELECT * FROM表WHERE $语句)) 或死亡$ DBI :: errstr; $ sth-> execute(); ((my(@rows))= $ sth-> fetchrow_array) { $ total_row_count = $ sth-> rows; $ database_rows = join(“\ |”,@rows); push(@ database_rows,$ database_rows); }
重新(3),你怀疑是否正确。 'AND'具有比'OR'更高的优先级,因此“确切”搜索子句将需要parens。 – ikegami 2012-02-14 05:48:48
谢谢。修改后的代码很好用。只是不知道如何发布它... – 2012-02-15 17:22:07