如何在一个字符串中请求多个查询?

时间:2014-08-26 19:34:58

标签: php sql boolean-logic

林'不完全确定我问了正确的问题,但也许如果你看到代码它可能更好地解释我试图做的事情。

因此,下面的查询会侦听上一页的帖子信息:

<?php    
  if (isset($_REQUEST["water-selection"])
      || isset($_REQUEST["city-selection"])
      || isset($_REQUEST["bed-selection"])
      || isset($_REQUEST["bath-selection"])
      || isset($_REQUEST["keyword"])
      || isset($_REQUEST["price"])
      || isset($_REQUEST["pending"])){
    $where = " WHERE 1=1 ";
    if (isset($_REQUEST["water-selection"])
        && $_REQUEST["water-selection"]!="Water Selection"){
        if ($where != ""){ $where .= " AND ";}
        $where .= " field_RESIBDWR = '" . $_REQUEST["water-selection"] . "'";
    }
    if (isset($_REQUEST["city-selection"])
        && $_REQUEST["city-selection"]!="City Selection"){
        if ($where != ""){ $where .= " AND ";}
        $where .= " field_City = '" . $_REQUEST["city-selection"] . "'";
    }
    if (isset($_REQUEST["bed-selection"]) 
        && $_REQUEST["bed-selection"]!="Bed(s)"){
        if ($where != ""){ $where .= " AND ";}
        $where .= " field_Bedrooms >= '" . $_REQUEST["bed-selection"] . "'";
    }
    if (isset($_REQUEST["bath-selection"])
        && $_REQUEST["bath-selection"]!="Bath(s)"){
        if ($where != ""){ $where .= " AND ";}
        $where .= " field_Bathrooms >= '" . $_REQUEST["bath-selection"] . "'";
    }
    if (isset($_REQUEST["keyword"])){
        if ($where != ""){ $where .= " AND ";}
        $where .= " (field_city like '%" . $_REQUEST["keyword"] . "%'
        OR field_State like '%" . $_REQUEST["keyword"] . "%'
        OR field_ZipCode like '%" . $_REQUEST["keyword"] . "%'
        OR field_MLNumber like '%" . $_REQUEST["keyword"] . "%'
        OR field_RESIBDWR like '%" . $_REQUEST["keyword"] . "%'
        OR field_ListingOfficeName like '%" . $_REQUEST["keyword"] . "%'
        OR field_RESIADDI like '%" . $_REQUEST["keyword"] . "%'
        OR field_MarketingRemarks like '%" . $_REQUEST["keyword"] . "%'
        OR field_StreetNumber like '%" . $_REQUEST["keyword"] . "%'
        AND field_StreetDirection like '%" . $_REQUEST["keyword"] . "%'
        AND field_StreetName like '%" . $_REQUEST["keyword"] . "%'
        AND field_StreetSuffix like '%" . $_REQUEST["keyword"] . "%')";
    }
    if (isset($_REQUEST["price"])){
        $price = explode(";",urldecode($_REQUEST["price"]));
        if ($where != ""){ $where .= " AND ";}
        $where .= " field_ListingPrice between " . $price[0] . "
                  AND " . $price[1] . " ";
    }
    if (isset($_REQUEST["pending"])){
        if ($where != ""){ $where .= " AND ";}
        $where .= " field_Status = 'Pending'";
    }
  }
  else{
    $where = " WHERE `field_ZipCode` IN (SELECT zip FROM zipcodes)";
  }

  $sql = "SELECT count(id) as count FROM rc_Data $where";
  $res = mysql_query($sql,$con) or
         die('Query failed: ' . mysql_error() . "<br />\n$sql");

  $records_per_page = 10;

  while($row = mysql_fetch_array($res)){
    $total_rec = $row["count"]; 
  }

  $total_pages = ceil($total_rec / $records_per_page);

  $curr_page = 1;

  if (isset($_REQUEST["page"])){$curr_page=$_REQUEST["page"];}

  $start_index = ($curr_page-1) * $records_per_page; 
  if ($start_index  <0) {$start_index = 0;}
  //$sql = "SELECT *, round(3956 *2 * ASIN( SQRT( POWER( SIN( ( {$lati} - 
           field_Latitude ) * pi( ) /180 /2 ) , 2 ) + COS
           ( {$lati} * pi( ) /180 ) * COS( field_Latitude * pi( ) /180 ) *
           POWER( SIN( ( {$longi} - field_Longitude) * pi( ) /180 /2 ) ,
           2 ) ) ),0) AS distance FROM rc_Data order by distance,
           (`field_ListingAgentMLSID` = 'H10207') DESC,
           `field_ListingAgentMLSID`, field_ListingPrice DESC, id desc limit
           {$start_index}, {$records_per_page}";
  //$sql = "SELECT *, round(3956 *2 * ASIN( SQRT( POWER( SIN( ( {$lati} -
           field_Latitude ) * pi( ) /180 /2 ) , 2 ) + COS( {$lati} * pi( )
           /180 ) * COS( field_Latitude * pi( ) /180 ) * POWER( SIN( (
           {$longi} - field_Longitude) * pi( ) /180 /2 ) , 2 ) ) ),0) AS
           distance FROM rc_Data order by (`field_ListingAgentMLSID` =
           'H10207') DESC, `field_ListingAgentMLSID`, distance,
           field_ListingPrice DESC, id desc limit {$start_index},
           {$records_per_page}";
  $sql = "SELECT *, round(3956 *2 * ASIN( SQRT( POWER( SIN( ( {$lati} -
         field_Latitude ) * pi( ) /180 /2 ) , 2 ) + COS( {$lati} * pi( )
         /180 ) * COS( field_Latitude * pi( ) /180 ) * POWER( SIN( (
         {$longi} - field_Longitude) * pi( ) /180 /2 ) , 2 ) ) ),0) AS
         distance FROM rc_Data  $where order by (`field_ListingAgentMLSID`
         = 'H10207') DESC, field_ListingPrice DESC limit {$start_index},
         {$records_per_page}";
  $res = mysql_query($sql,$con) or
         die('Query failed: ' . mysql_error() . "<br />\n$sql");
  //echo $sql;

  unset($_GET['page']);
  $qs = http_build_query($_GET);
?>

帖子网址的示例如下:

[site]/listing.php?keyword="321+STREETNAME+STREETDirection+City+State&......

如果我单独执行某些操作,例如"321",它会正确搜索321,但是,如何才能搜索"321" + "Streetname" + City + State,等...

希望这是有道理的。我基本上不能用它来搜索多个匹配。

我在想可能会将OR更改为AND,但之后如何将其他关键字分开?

2 个答案:

答案 0 :(得分:0)

<?php
$where = '';

if (isset($_GET['keyword']) && '' !== trim($_GET['keyword'])) {
  $terms = explode(' ', $_GET['keyword']);
  $fields = array('city', 'state', 'zipcode');
  $criteria = array();
  foreach ($terms as $term) {
    if (!$term) continue;
    $term = @mysql_real_escape_string($term);
    $c = array();
    foreach ($fields as $field) {
      $c[] = 'field_'.$field.' LIKE \'%'.$term.'%\'';
    }

    $criteria[] = implode(' OR ', $c);
  }

  if ($criteria) {
    $condition = '('.implode(")\nAND (", $criteria).')';
    if ($where) $where .= ' AND ('.$condition.')';
    else $where = $condition;
  }
}

var_dump($where);

请使用转义函数或更好的预处理语句来处理SQL注入问题。此外,此解决方案远非最佳,您应考虑限制可搜索的字段(例如,使用下拉列表)。

答案 1 :(得分:0)

尝试使用explode()功能。您将获得一系列关键字。

$keywords = $_REQUEST["keyword"];
$arrayOfKeywords = explode('+', $keywords)