多个查询条件的sql语句的拼写技巧,求指点。
$sql="select * from tb1";if($id=$_GET['id']){ $where.=" where id like "%$id%"";}if($name=$_GET['name']){ $where.=" where name like "%$name%"";}//当id有值的时候sql=select * from tb1 where id like "%$id%"//当name有值的时候sql=select * from tb1 where name like "%$name%"//当同时又值的时候,sql就出错了sql=select * from tb1 where name like "%$name%" where where id like "%$id%" //当然你可以说用if($id=$_GET['id']&&$name=$_GET['name']){ where.= "and";}
$where = array();foreach($_GET as $k=>v) $where[] = "$k like '%$v%'";$sql="select * from tb1";if($where) $sql .= ' where ' . join(' and ', $where);
$sql="select * from tb1";$where = array();if($id=$_GET['id']){ $where[]=" id like '%$id%'";}if($name=$_GET['name']){ $where[]=" name like '%$name%'";}$s=(!empty($where)) ? " where " . implode(" and " , $where) : '';$sql.=$s;
我个人觉得我这个简单
$sql="select * from tb1 where 1=1";if($id=$_GET['id']) $sql.=" and id like "%$id%"";if($name=$_GET['name']) $sql.=" and name like "%$name%"";
我个人觉得我这个简单
$sql="select * from tb1 where 1=1";if($id=$_GET['id']) $sql.=" and id like "%$id%"";if($name=$_GET['name']) $sql.=" and name like "%$name%"";