小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

PHP多条件查询处理

PHP多条件查询处理

在SQL里面写这种查询的地方很多,要让查询写得富化。
//方法一
//获取字典类型

$sztm=trim($_REQUEST['ztm']);
$stmpy=trim($_REQUEST['tmpy']);
$sssh=trim($_REQUEST['ssh']);
$scbm=trim($_REQUEST['cbm']);

 

$sql = 'select * from ts_gcb ';

if ( $sztm != '' ){
 $sql  .= ' where ztm like "%'.$sztm.'%" ';
}

if ( $stmpy != '' ){
 $sql .= ( preg_match('/where/' , $sql ) ? ' and ': ' where ' ) . ' tmpy like "%'.$stmpy.'%" ';
}

if ( $sssh != '' ){
 $sql .= ( preg_match('/where/' , $sql ) ? ' and ': ' where ' ) . ' ssh like "%'.$sssh.'%" ';
}

if ( $scbm!= '' ){
 $sql .= ( preg_match('/where/' , $sql ) ? ' and ': ' where ' ) . ' cbm like "%'.$scbm.'%" ';
}


echo $sql;

$conn = mysql_connect($dbhost, $dbuser, $dbpwd);
mysql_select_db($dbname);
$rst = mysql_query($sql, $conn);


///方法二


if(empty($isbn))
{
   $where[]="`isbn` like '%$isbn%'";
}

implode(' and ',$where);

posted on 2008-08-31 11:48  王峰炬  阅读(134)  评论(0编辑  收藏  举报

导航