谢谢楼上,但我在写SQL时总得加上条件是吧,如果按姓名 where 姓名 like xxx ,那如果姓名为空,或期它条件为空怎么办呢?
sql语句只需要一条,但是where子句是要用多个if来组织起来的.例如 $Where = " where 1 "; if($_POST['name']<>"") $Where = " and name like '%".$_POST['name']."%'"; if(...) $Where = " and ..........";最后把这个Where 子句接在sql语句上并加排序或分页就可以了 $sql = "Select ... from ... ".$Where." order by .. limit .."
if(性别不为空) { sql += " and sex=sex"; } 看到没有啊..如果不为空才+ 如果为空它根本不会+啊..
var sql="...... whrer 1 = 1";
if(性别不为空)
{
sql += " and sex=sex";
}
if(爱好不为空)
{
sql+= "and 爱好 = 爱好";
}这样一个个来
$Where = " where 1 ";
if($_POST['name']<>"")
$Where = " and name like '%".$_POST['name']."%'";
if(...)
$Where = " and ..........";最后把这个Where 子句接在sql语句上并加排序或分页就可以了
$sql = "Select ... from ... ".$Where." order by .. limit .."
{
sql += " and sex=sex";
}
看到没有啊..如果不为空才+
如果为空它根本不会+啊..