doulu3399 2010-11-11 17:42
浏览 15
已采纳

php,sql有趣的查询选项

    <input type="checkbox" name="average" value="average" <? if (get_option('average') == 'average'){ echo 'checked="checked"';} ?>>Average

    <input type="checkbox" name="petite" value="petite" <? if (get_option('petite') == 'petite'){ echo 'checked="checked"';} ?>>Petite



     if ( get_option('average') == 'average' ): // choose category
    $average = "AND build = '".get_option('average')."'";
    endif;

    if ( get_option('petite') == 'petite' ): // choose category
    $petite = "OR build = '".get_option('petite')."'";
    endif;


    $qry = mysql_query("SELECT performerid,pic0 FROM ".$table." WHERE **$average $petite** ORDER BY RAND() LIMIT 20");

How can i make this code work? I need the sql query to work if $average is selected, is both $average and $petite are selected, or just $petite is selected?

TY!

  • 写回答

2条回答 默认 最新

  • douhuan3448 2010-11-11 17:50
    关注

    you may use something like this:

    $selectedCategories = array();
    foreach(array('petite', 'average', 'athletic') as $category)
    {
      if (get_option($category) == $category)
      {
        $selectedCategories[] = $category;
      }
    }
    
    $qry = mysql_query(
     "SELECT performerid,pic0 
        FROM ".$table." 
       WHERE build IN('" . implode("', '", $selectedCategories) . "') 
    ORDER BY RAND() LIMIT 20;");
    

    Note: this won't work if you select no category.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度