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

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

WBOY
Release: 2016-06-06 20:30:37
Original
1687 people have browsed it

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

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

回复内容:

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

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

直接上代码:

<code>$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}");
</code>
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template