2015-02-23 65 views
0

例子:MySQL查詢+ ASP的if else end如果

Sql = "Select * From tablo Where Baslik = "&strBaslik&" And Ozellik IN ("&strOzellik&") Order By Id Desc" 

我想:

Sql = "Select * From tablo Where Baslik = "&strBaslik&" **IF Not "&strOzellik&" = Then** And Ozellik IN ("&strOzellik&") **End IF** Order By Id Desc" 

我該怎麼辦呢?

謝謝。

回答

0

有幾種方法來解決這個問題,其中之一是連接的sSql查詢:

<% 
sSql = "Select * From tablo Where Baslik = '"& strBaslik & "' " 
If Not strOzellik = "???" Then 
    sSql = sSql & " And Ozellik IN ('" & strOzellik & "')" 
End If 
sSql = sSql & " Order By Id Desc" 
%> 

請注意,我還添加了周圍的情況下,你的SQL參數單引號的投入實際上是字符串。雖然,這種方式並不安全,因爲SQL注入,你可能想看看parameterized queries

+0

非常感謝。 – Alp 2015-02-24 10:17:24

+0

@Alp樂於提供幫助,歡迎來到Stack Overflow。如果此答案或任何其他人解決了您的問題,請將其標記爲已接受。 – 2015-02-25 01:22:20