PHP and MySQL optional WHERE conditions

后端 未结 4 2132
心在旅途
心在旅途 2021-01-29 09:59

I have the following problem: I want to let a user apply filters to a DB search. I have three filters, A, B and C. All of them can be \"empty\", as in, the user doesn\'t care a

4条回答
  •  逝去的感伤
    2021-01-29 10:02

    Before your query you could use:

    $tmp = "where ";
    if($A and $A!="any" and $A!="not used")
     $tmp .= "row1 = '".$A."'";
    if($B and $B!="any" and $B!="not used")
     $tmp .= "AND row2 = '".$B. "'";
    if($C and $C!="any" and $C!="not used")
     $tmp .= "AND row3 = '".$C."'";
    $db_q = "Select * from table $tmp";
    

提交回复
热议问题