douyue8191 2011-02-23 15:39
浏览 186
已采纳

如何使用多个WHERE子句执行MySQL查询?

how would you do a mysql query where a user can choose from multiple options. Fox example I have a form that user can use to search for houses. Now I have a select box where you can chosse whether you want a house, a flat or whatever. Then I have a second box where you can choose for example the city you want the house or flat to be in. And maybe another one with the maximum price.

Now how would you do the mysql query? My problem is, I would do it like that: if($_POST["house_type"] != 0) { $select = mysql_query("SELECT * FROM whatever WHERE type = '".$_POST["house_type"]."'"); } But now I only have the case that someone has chosen a house type but not any other option. So do I have to do an "if" for every possible combination of selected elements?

To emphasize my problem:

if(!isset($_POST["house_type"])) {

            if($_POST["something"] == 0) {
        $search_select = @mysql_query("SELECT * FROM housedata WHERE something = $_POST["whatever"]);

            }
            elseif($_POST["something"] != 0) {
        $search_select = @mysql_query("SELECT * FROM housedata  something = $_POST["whatever"] AND somethingelse = 'whatever');
            }
        }



elseif(!isset($_POST["house_type"])) {


        if($_POST["something"] == 0) {
        $search_select = @mysql_query("SELECT * FROM housedata WHERE something = $_POST["whatever"]);

            }
            elseif($_POST["something"] != 0) {
        $search_select = @mysql_query("SELECT * FROM housedata  something = $_POST["whatever"] AND somethingelse = 'whatever');
            }
        }

Now imagine I had like 10 or 20 different select boxes, input fields and checkboxes and I would have to do a mysql query depending on what of these boxes and fiels and checkboxes is filled. This would be a code that is extremely complicated, slow and horrible. So is there a possibility to make a mysql query like:

SELECT * FROM whatever WHERE house_data = '".$whatever."' AND (if(isset($_POST["something"])) { whatever = '".$whatever2."' } AND ...;

You get what I mean? Its a bit complicated to explain but actually its a very important question and probably easy to answer.

Thank you for your help! phpheini

  • 写回答

5条回答 默认 最新

  • doukong1897 2011-02-23 15:46
    关注

    Generate the WHERE clause prior to running the SQL.

    A short example:

    $whereClause = "";
    
    if ($_POST['opt1']) {
        $opt1 = mysql_real_escape_string($_POST['opt1']);
        $whereClause .= "AND opt1='$opt1'";
    }
    
    if ($_POST['opt2']) {
        $opt2 = mysql_real_escape_string($_POST['opt2']);
        $whereClause .= "AND opt2='$opt2'";
    }
    
    mysql_query("SELECT * FROM table WHERE 1 ".$whereClause); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料