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);

原文地址:https://www.cnblogs.com/fengju/p/6174014.html