Home  >  Article  >  Backend Development  >  How to implement multi-condition fuzzy query in PHP

How to implement multi-condition fuzzy query in PHP

藏色散人
藏色散人Original
2021-06-21 09:08:342744browse

php method to implement multi-condition fuzzy query: first receive the post value; then pass "if(!empty($name)){$where['name'] = array('like','% '.$name.'%')..." method encapsulates the fuzzy query and assigns it to an array.

How to implement multi-condition fuzzy query in PHP

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

PHP Fuzzy Query (Multiple Conditions)

The code is as follows:

                //<!--php模糊查询 -->
        //接收post传值
$name = I(&#39;post.name&#39;);  
$mail = I(&#39;post.mail&#39;);  
$age = I(&#39;post.age&#39;);  
if(!empty($name)){  
                //封装模糊查询 赋值到数组 
$where[&#39;name&#39;] = array(&#39;like&#39;,&#39;%&#39;.$name.&#39;%&#39;); 
}  
if(!empty($mail)){  
$where[&#39;mail&#39;] = array(&#39;like&#39;,&#39;%&#39;.$mail.&#39;%&#39;);  
}  
if(!empty($age)){  
$where[&#39;age&#39;] = $age;  
} 
                //如果查询条件是OR的关系请打开,一般都是AND关系。 
$where[&#39;_logic&#39;]=&#39;OR&#39;;  
$test = M(&#39;table&#39;)->where($where)->select();

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to implement multi-condition fuzzy query in PHP. For more information, please follow other related articles on the PHP Chinese website!

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