Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php搜索帮助_Php_Mysql - Fatal编程技术网

php搜索帮助

php搜索帮助,php,mysql,Php,Mysql,以下是我从搜索表单中发布的变量: $city = $_REQUEST['city']; $location = $_REQUEST['location']; $bedrooms = $_REQUEST['noofbedrooms']; $addeddate = $_REQUEST['addeddate']; $minprice = $_REQUEST['pricefrom']; $maxprice = $_REQUEST['priceto']; $minarea = $_REQUEST['are

以下是我从搜索表单中发布的变量:

$city = $_REQUEST['city'];
$location = $_REQUEST['location'];
$bedrooms = $_REQUEST['noofbedrooms'];
$addeddate = $_REQUEST['addeddate'];
$minprice = $_REQUEST['pricefrom'];
$maxprice = $_REQUEST['priceto'];
$minarea = $_REQUEST['areafrom'];
$maxarea = $_REQUEST['areato'];
$propertytype = $_REQUEST['proptype'];
到目前为止还不错。现在,我需要一些关于以下场景的好建议。 我的领域中几乎每个元素都是可选的。这意味着我可以在上面的变量中得到空值

为上述变量创建mysql查询的场景应该是什么。在这种情况下,我可以为每个场景使用条件。乙二醇

if($city=="")
  $query="";
elseif($location=="")
  $query="";
and so on....

我需要一些专业的方法来解决这个问题。

你可以这样做

$where = array();

if(strcmp($_REQUEST['city'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['city'] . "'";
}
if(strcmp($_REQUEST['location'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['location'] . "'";
}
if(strcmp($_REQUEST['noofbedrooms'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['noofbedrooms'] . "'";
}
if(strcmp($_REQUEST['addeddate'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['addeddate'] . "'";
}
if(strcmp($_REQUEST['pricefrom'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['pricefrom'] . "'";
}
...... // check for all the fields

when create the SQL using 

$SQL = implode(" OR ", $where);
之后,您可以在一些SQL上使用它,比如

"SELECT * FROM WHERE {$SQL}";

你可以这样做

$where = array();

if(strcmp($_REQUEST['city'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['city'] . "'";
}
if(strcmp($_REQUEST['location'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['location'] . "'";
}
if(strcmp($_REQUEST['noofbedrooms'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['noofbedrooms'] . "'";
}
if(strcmp($_REQUEST['addeddate'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['addeddate'] . "'";
}
if(strcmp($_REQUEST['pricefrom'], "")){
    $where[] = "SEARCH_COLUMN = '" . $_REQUEST['pricefrom'] . "'";
}
...... // check for all the fields

when create the SQL using 

$SQL = implode(" OR ", $where);
之后,您可以在一些SQL上使用它,比如

"SELECT * FROM WHERE {$SQL}";

如果所有查询部分都以相同的方式构建:WHERE fieldname='fieldvalue',则可以使用一种基于循环的惰性方法:

$conditions = array();

foreach (array("city", "location", "noofbedrooms") as $field) 
           // ^ add all fields as needed
 {
   // Check whether parameter was passed at all
   if (!array_key_exists($field, $_POST)) continue;

   // Check whether parameter is empty
   if (!empty($_POST[$field]))
    $conditions[]="`$field` = ".mysql_real_escape_string($_POST[$field]);  
                                // ^ or whatever your database library 
                                //   does for escaping  

 }

 $query = "SELECT * from table where ".implode(" AND ", $conditions); 

如果所有查询部分都以相同的方式构建:WHERE fieldname='fieldvalue',则可以使用一种基于循环的惰性方法:

$conditions = array();

foreach (array("city", "location", "noofbedrooms") as $field) 
           // ^ add all fields as needed
 {
   // Check whether parameter was passed at all
   if (!array_key_exists($field, $_POST)) continue;

   // Check whether parameter is empty
   if (!empty($_POST[$field]))
    $conditions[]="`$field` = ".mysql_real_escape_string($_POST[$field]);  
                                // ^ or whatever your database library 
                                //   does for escaping  

 }

 $query = "SELECT * from table where ".implode(" AND ", $conditions); 

这样做应该可以:

$fields = array('city', 'location' /* ... */);
$conditions = array();

foreach ($fields as $field) {
    if (!empty($_REQUEST[$field])) {
        $conditions[] = 'column LIKE "%'.mysql_real_escape_string($_REQUEST[$field]).'%"';
    }
}

$query = 'SELECT row FROM table WHERE '.implode(' OR ', $conditions);

您需要调整此选项以满足您的需要,例如。

类似的操作应该可以:

$fields = array('city', 'location' /* ... */);
$conditions = array();

foreach ($fields as $field) {
    if (!empty($_REQUEST[$field])) {
        $conditions[] = 'column LIKE "%'.mysql_real_escape_string($_REQUEST[$field]).'%"';
    }
}

$query = 'SELECT row FROM table WHERE '.implode(' OR ', $conditions);

您需要调整此字段以满足您的需要,例如。

我在上面回答过,我认为由于您的字段是可选的,您不需要使用SQL,而且,有些时候=也不太好,您可以在大多数情况下使用LIKE,如location,price from可以是a>=等。请注意

我在上面回答了,我认为由于您的字段是可选的,您不需要使用SQL,而且,有时=也不太好,您可以在大多数情况下使用LIKE,例如location,price from可以是>=等。请注意

嗯,在我看来,您可以将表字段的默认值设置为NULL,然后插入表值“{$\u REQUEST['city']}”……根据标题,我认为这是关于选择而不是插入。在我看来,您可以将表字段的默认值设置为NULL,然后插入表值“{$\u REQUEST['city']}”,…根据标题,我认为这是关于选择而不是插入。我随意添加了mysql\u real\u escape\u字符串,mysql\u escape\u字符串不受欢迎。我随意添加了mysql\u real\u escape\u字符串,mysql\u escape\u字符串不受欢迎