Home  >  Article  >  Backend Development  >  javascript - php多条件筛选查询怎么做

javascript - php多条件筛选查询怎么做

WBOY
WBOYOriginal
2016-06-06 20:30:371550browse

javascript - php多条件筛选查询怎么做

就像这样,多个条件检索怎么做,求大神指点

回复内容:

javascript - php多条件筛选查询怎么做

就像这样,多个条件检索怎么做,求大神指点

直接上代码:

$query = [];
$valid = ['type', 'area', 'year', 'person']; // 有效的查询Key:类型/地区/年代/明星

foreach($_GET AS $queryName) {
    // 查询全部时,直接忽略该条件
    if( ($_GET[$queryName] !== 'all') && in_array($queryName, $valid) ) {
        $query[$queryName] = $_GET[$queryName];
    }
}

$where = count($query) ? 'WHERE '.implode(',', $query) : '';

$result = $db->query("SELECT * FROM tableName {$where}");

这些条件就是一个分类啊。
各种分类下的交集就好了。
mysql下用where语句就好,用and连接

拼接sql语句的活。设置一个数组$search,保存提交过来的条件值,比如$search['type']=$_GET['type'],把每个条件都赋值一次。在拼接sql之前在对每个条件进行判断,比如if($search['type']) $query .= ' AND type=?';$bind[] = $search['type'];
当条件不为空时拼接sql,否则就不拼接。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn