具有多个条件的 PHP MySQL 搜索
Posted
技术标签:
【中文标题】具有多个条件的 PHP MySQL 搜索【英文标题】:PHP MySQL search with multiple criteria 【发布时间】:2014-12-03 21:36:57 【问题描述】:我在网站上有一个搜索表单,希望有几个由用户输入的搜索词来执行数据库搜索,词如下:
关键字 物业(出售、出租...) 物业类型(公寓、排屋...) 状态 最低价格 最高价格这是使用上述术语的输入执行搜索的脚本
public function get_property_list_by_search($start, $per_page, $keyword, $prop_for, $min, $state, $ptype, $max, $mysqli)
if(empty($start) && empty($per_page))
return 0;
$start = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($start));
$per_page = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($per_page));
$keyword = $mysqli->real_escape_string(stripslashes($keyword));
$prop_for = $mysqli->real_escape_string(stripslashes($prop_for));
$state = $mysqli->real_escape_string(stripslashes($state));
$ptype = $mysqli->real_escape_string(stripslashes($ptype));
$min_price = self::num_clean($mysqli->real_escape_string($min));
$max_price = self::num_clean($mysqli->real_escape_string($max));
$t1 = '';
$t2 = '';
$t3 = '';
$t4 = '';
$t5 = '';
if(isset($keyword) && !empty($keyword))
$t1 = " AND `proj_title` LIKE '%".$keyword."%' OR `proj_addr` LIKE '%".$keyword."%' OR `proj_area` LIKE '%".$keyword."%'";
if(isset($prop_for) && !empty($prop_for))
$t2 = " AND `proj_for`='".$prop_for."'";
if(isset($state) && !empty($state))
$t3 = " AND `state`='".$state."'";
if(isset($ptype) && !empty($ptype))
$t4 = " AND `proj_cat`='".$ptype."'";
//min & max
if((isset($min_price) && !empty($min_price)) && (isset($max_price) && !empty($max_price)))
$t5 = " AND `price` BETWEEN '".$min_price."' AND '".$max_price."'";
//min only
if(!empty($min_price) && empty($max_price))
$t5 = " AND `price` >= '".$min_price."'";
//max only
if(empty($min_price) && !empty($max_price))
$t5 = " AND `price` <= '".$max_price."'";
$sql = $mysqli->query("SELECT * FROM `project` WHERE `status`='1' ".
$t1." ".$t2." ".$t3." ".$t4." ".$t5." ".
"ORDER BY `posted_date` DESC LIMIT ".$start.", ".$per_page);
if($sql->num_rows > 0)
return $sql;
else
return false;
查询输出将类似于:
SELECT * FROM `project`
WHERE `proj_title` LIKE '%keywords%'
OR `proj_addr` LIKE '%keywords%'
OR `proj_area` LIKE '%keywords%'
AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000'
(price
的数据类型是DECIMAL(10,2)
,它存储的值类似于250000.00
)
但是,返回的结果并不像预期的那样(不准确),它也会出现价格超过 600000 和项目类别超出 '8' 的结果,最终用户不喜欢搜索网站。
有什么方法可以优化查询以执行更具体的操作吗?
【问题讨论】:
【参考方案1】:您应该使用“.=" 运算符,而不是使用这些变量。
/* $t1 = '';
$t2 = '';
$t3 = '';
$t4 = '';
$t5 = '';
*/
$q = "SELECT * FROM `property` WHERE `status`='1' ";
// You need to enclose all **OR** logical tests in parenthesis.
// Moreover most of the usages of isset function are useless,
// as your are initializing many variables
if($keyword && !empty($keyword))
$q .= " AND (`p_title` LIKE '%".$keyword."%' OR `address` LIKE '%".$keyword."%' OR `area` LIKE '%".$keyword."%')";
if($prop_for && !empty($prop_for))
// If you are using double quotes you really don't need handle to concatenation.
$q .= " AND `p_for`='$prop_for'";
if($state && !empty($state))
$q .= " AND `state`='$state'";
if($ptype && !empty($ptype))
$q .= " AND `p_category`='$ptype'";
//min only
if($min_price && !empty($min_price))
$q .= " AND `price` >= '".$min_price."'";
//max only
if($max_price && !empty($max_price))
$q .= " AND `price` <= '$max_price'";
// When you are not using OFFSET keyword,
//the first number after LIMIT keyword should be the number of records
$q .= " ORDER BY `posted_date` DESC LIMIT $per_page , $start;";
$sql = $mysqli->query($q);
【讨论】:
谢谢伙计。我可以向你学习吗?如何输出查询以检查班级的目的?我仍然不知道导致失败的查询出了什么问题。 哦,当然@conmen,:) 你可以在 FB(fb.com/gahse) 上加我。我只是要求您添加 echo $q;在此行之前 $sql = $mysqli->query($q);这将返回查询字符串,然后您可以复制此字符串并将其粘贴到任何 MySql 客户端,如 phpmyadmin 或 sqlyog。它将帮助您消除所有语法错误。【参考方案2】:你需要括号。
SELECT * FROM `project` WHERE (`proj_title` LIKE '%keywords%' OR `proj_addr` LIKE '%keywords%' OR `proj_area` LIKE '%keywords%') AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000'
没有括号,它只需要匹配最后一个 OR 之前的条件之一。
【讨论】:
【参考方案3】:if(isset($_SESSION['login']))
echo "<div align=\"right\"><strong><a href=\"index.php\"> Home </a>|
<a href=\"signout.php\">Signout</a>|
<a href=\"home.php\">Profile</a></strong></div>";
else
echo " ";
$con= mysql_connect("localhost","root","");
$d=mysql_select_db("matrimonial",$con);
$gender=$_POST['gender'];
$age1=$_POST['age1'];
$age2=$_POST['age2'];
$city=$_POST['city'];
$subcast=$_POST['subcast'];
$result=mysql_query("select * from matri where gender='$gender' and age between '$age1' and '$age2' and city='$city' and subcast='$subcast'");
if($gender && !empty($gender))
$result .= " AND `gender`='$gender'";
if($age1 && !empty($age1))
$result .= " AND `age`='$age1'";
if($age2 && !empty($age2))
$result .= " AND `age`='$age2'";
if($city && !empty($city))
$result .= " AND `city`='$city'";
if($subcast && !empty($subcast))
$result .= " AND `subcast`='$subcast'";
$result .= " select * from ";
$sql = $mysql->query($result);
how to run this code
【讨论】:
我想通过用户选择或不选择来搜索此字段。如果不选择而不是按搜索条件选择所有其他选项,我该怎么做【参考方案4】:关于价格差异,如果价格在 2 个值之间,则您应该做一个,否则只有 1 个值。
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。以上是关于具有多个条件的 PHP MySQL 搜索的主要内容,如果未能解决你的问题,请参考以下文章