Home > Backend Development > PHP Tutorial > thinkphp implements like fuzzy query example

thinkphp implements like fuzzy query example

不言
Release: 2023-03-30 07:10:01
Original
8055 people have browsed it

This article mainly introduces the implementation of like fuzzy query in thinkphp. It uses examples to describe the implementation method of like fuzzy query in string form and array form as query conditions. It is a very practical skill. Friends who need it can refer to it.

The example in this article describes how thinkphp implements like fuzzy query, and shares it with you for your reference. The specific implementation method is as follows:

Currently, more and more people are using the thinkphp framework for project development. Due to its good encapsulation, many parts of pure PHP development are not easy to get started. The examples in this article are blurred with like Query is an example to illustrate this.

Here we mainly use examples to illustrate usage:

ThinkPHP can support the direct use of strings as query conditions, but in most cases it is recommended to use index arrays or objects as query conditions, because it will be safer .

1. Use strings as query conditions

This is the most traditional method, but it is not very safe.
For example:

Copy code The code is as follows:

$User = M("User"); // 实例化User对象
$User->where('type=1 AND status=1')->select();
Copy after login

The final generated SQL statement is

Copy code The code is as follows:

SELECT * FROM think_user WHERE type=1 AND status=1
Copy after login

If you perform a multi-field query, then the fields The default logical relationship between them is logical AND AND, but the default logical judgment can be changed using the following rules, by using _logic to define query logic:

Copy code The code is as follows:

$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// 把查询条件传入查询方法
$User->where($condition)->select();
Copy after login

The final generated SQL statement is

Copy code The code is as follows:

SELECT * FROM think_user WHERE `name`='thinkphp' OR `account`='thinkphp'
Copy after login

2. Array mode as query condition

As mentioned How to implement so many like queries? Let’s see below

Copy code The code is as follows:

$userForm=M('user'); 
$where['name']=array('like','php%');
$userForm->where($where)->select();
Copy after login

The like query here is:

Copy code The code is as follows:

name like 'php%'
Copy after login

Query statement:

Copy code The code is as follows:

$where['name']=array('like',array('%php%','%.com'),'OR');
Copy after login

The like query here is:

Copy code The code is as follows:

name like '%php%' or name like '%.com'
Copy after login

Query statement:

Copy code The code is as follows:

$where['name']=array(array('like','%a%'),array('like','%b%'),array('like','%c%'),'php','or');
Copy after login

The like query here is:

Copy code The code is as follows:

(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'php')
Copy after login

Query statement:

Copy code Code As follows:

$where['_string']='(name like "%php%")  OR (title like "%php")';
Copy after login

The like query here is:

Copy code The code is as follows:

name like '%jb51%' or title like '%php'
Copy after login

Related recommendations:

thinkphp implements 163, QQ mailbox method of sending and receiving emails_php skills

ThinkPHP basic add, delete, check and modify operation example tutorial

The above is the detailed content of thinkphp implements like fuzzy query example. For more information, please follow other related articles on the PHP Chinese website!

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