mysql 动态查询条件

[复制链接]
查看11 | 回复1 | 2010-4-7 08:10:58 | 显示全部楼层 |阅读模式
为什么要排拆IF呢?下面这样能达到你的要求么?$where_clause ="";
if(!empty($_POST['name'])) $where_clause .= " and name like '%".$_POST['name']."%'";
if(!empty($_POST['age'])) $where_clause .= " and name like '%".$_POST['age']."%'";
if(!empty($_POST['sex'])) $where_clause .= " and name like '%".$_POST['sex']."%'";
$sql = "select * from userwhere 1=1 ".$where_clause;以上语句是在记事本里写的,未做语法检查.另外你可以不用like,而用=
回复

使用道具 举报

千问 | 2010-4-7 08:10:58 | 显示全部楼层
用if语句判断如果年龄不为空就在查询语句中设置and age=。。以此类推ifelse ifelse ifelse
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行