我是新手。我搜索了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