php网站,查询条件:姓名,性别,年龄,职位,政治面貌,职称,单位。求sql语句实现多条件查询

2025-03-06 11:21:56
推荐回答(4个)
回答1:

$name,$sex,$age,$posi,$part,$zhic,$danwei
这些都是接过来的值,我直接写语句了
$where = '1=1';
if($name){
$where1 .= "and name = '$name'";
}
if($sex){
$where2 .= "and sex= '$sex";
}
if($age){
$where3 .= "and age= '$age";
}
if($posi){
$where4 .= "and posi= '$posi";
}
if($part){
$where5 .= "and part= '$part";
}
if($zhic){
$where6 .= "and zhic= '$zhic";
}

if($danwei){
$where7 .= "and danwei= '$danwei";
}
$where .='$where1'$where2'$where3'$where4'$where5'$where6'$where7
$sql = "select * from 表名 where $where";

这样写行吗??

回答2:

$name,$sex,$age,$posi,$part,$zhic,$danwei
这些是客户端传过来的值,有必要,你验证一下,安全。

$where = '1';
if($name){
$where .= "and name = '".$name."'";
}
if($sex){
$where .= "and sex= '".$sex."‘";
}
if($age){
$where .= "and age= '".$age."’";
}
if($posi){
$where .= "and posi= '".$posi."‘";
}
if($part){
$where .= "and part= '".$part."’";
}
if($zhic){
$where .= "and zhic= '".$zhic."‘";
}

if($danwei){
$where .= "and danwei= '”.$danwei."’";
}

$sql = "select * from 表名 where $where";

回答3:

$name,$sex,$age,$posi,$part,$zhic,$danwei
这些都是接过来的值,我直接写语句了
$where = '1=1';
if($name){
$where .= "and name = '$name'";
}
if($sex){
$where .= "and sex= '$sex";
}
if($age){
$where .= "and age= '$age";
}
if($posi){
$where .= "and posi= '$posi";
}
if($part){
$where .= "and part= '$part";
}
if($zhic){
$where .= "and zhic= '$zhic";
}

if($danwei){
$where .= "and danwei= '$danwei";
}

$sql = "select * from 表名 where $where";

回答4:

$where = '';
if(!empty($name)){
$where .= " AND name like '%$name%'";
}
if(!empty($sex)){
$where .= " AND name like '%$sex%'";

}

一次类推 $sql = "SELECT * FROM user WHERE 1 $where";