Home  >  Article  >  Backend Development  >  thinkphp implements like fuzzy query example

thinkphp implements like fuzzy query example

不言
不言Original
2018-06-06 14:08:318033browse

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();

The final generated SQL statement is

Copy code The code is as follows:

SELECT * FROM think_user WHERE type=1 AND status=1

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();

The final generated SQL statement is

Copy code The code is as follows:

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

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();

The like query here is:

Copy code The code is as follows:

name like 'php%'

Query statement:

Copy code The code is as follows:

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

The like query here is:

Copy code The code is as follows:

name like '%php%' or name like '%.com'

Query statement:

Copy code The code is as follows:

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

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')

Query statement:

Copy code Code As follows:

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

The like query here is:

Copy code The code is as follows:

name like '%jb51%' or title like '%php'

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!

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