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

How to implement multi-condition fuzzy query in PHP

藏色散人
Release: 2023-03-10 18:54:02
Original
2906 people have browsed it

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();
Copy after login

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!

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template