Home >Backend Development >PHP Tutorial >多条件查询有关问题

多条件查询有关问题

WBOY
WBOYOriginal
2016-06-13 12:21:55982browse

多条件查询问题

<br />$st=$_POST['st'];<br />$et=$_POST['et'];<br />$or_type=$_POST['or_type'];<br />$state=$_POST['state'];<br />//上面是个是传过来的多条件查询,有可能只有一个,也有可能两个<br />if($_POST) {<br />$sql="select * from order a left join user b on a.juserid=b.id where a.mbus_id='$bid' and <span style="color: #FF0000;">....这里是传过来的多条件,怎么写?有可能是3个条件,也有可能是3个条件</span>";<br />}<br />


------解决思路----------------------
先过滤掉空参数和不参与查询的参数
$param = array_diff($_POST, array('', ’其他需要过滤的值‘));

然后从 $param 组装查询串(假定传入的变量名就是字段名)
foreach($param as $k=>&$v) $v = "$k='$v'";
$where = join(' and ', $param);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn