Thinkphp implements like fuzzy query instances and thinkphp fuzzy query _ PHP Tutorial

Source: Internet
Author: User
Thinkphp implements like fuzzy query instances and thinkphp fuzzy query. Thinkphp implements like fuzzy query instances. this example describes how thinkphp implements like fuzzy query. The specific implementation method is as follows: thinkphp implements like fuzzy query instance and thinkphp fuzzy query

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

At present, more and more people are using thinkphp framework for project development. due to its good encapsulation, many pure PHP development tasks are not easy to use, this example uses like fuzzy search as an example to describe it.

Here we will give an example to illustrate the usage:

ThinkPHP supports using strings directly as query conditions. However, it is recommended that indexes or objects be used as query conditions in most cases, because they are more secure.

1. Use a string as the query condition

This is the most traditional method, but the security is not high,
For example:

The code is as follows:

$ User = M ("User"); // instantiate the User object
$ User-> where ('Type = 1 AND status = 1')-> select ();


The last generated SQL statement is

The code is as follows:

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

If multi-field query is performed, the default logical relationship between fields is logical AND. However, you can use the following rules to change the default logical judgment by using _ logic to define the query logic:

The code is as follows:

$ User = M ("User"); // instantiate the User object
$ Condition ['name'] = 'thinkphp ';
$ Condition ['account'] = 'thinkphp ';
$ Condition ['_ logic'] = 'OR ';
// Pass the query condition into the query method
$ User-> where ($ condition)-> select ();


The last generated SQL statement is

The code is as follows:

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

2. use the array method as the query condition

After talking about how to implement so many like queries, let's take a look.

The code is as follows:

$ UserForm = M ('user ');
$ Where ['name'] = array ('like', 'jb51% ');
$ UserForm-> where ($ where)-> select ();


The like query here is:

The code is as follows:

Name like 'jb51%'

Query statement:

The code is as follows:

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


The like query here is:

The code is as follows:

Name like '% jb51 %' or name like '%. com'

Query statement:

The code is as follows:

$ Where ['name'] = array ('like', '% a %'), array ('like', '% B % '), array ('like', '% c %'), 'jb51 ',' or ');


The like query here is:

The code is as follows:

('Name' LIKE '% a %') OR ('name' LIKE '% B %') OR ('name' LIKE '% c % ') OR ('name' = 'jb51 ')

Query statement:

The code is as follows:

$ Where ['_ string'] =' (name like "% jb51 %") OR (title like "% jb51 ")';


The like query here is:

The code is as follows:

Name like '% jb51 %' or title like '% jb51'

I hope this article will help you with PHP programming.


% {$ _ POST ['username']} % of SQL fuzzy query in thinkphp, Zhao Tong said. why is there {}?

If there are characters before and after the variable, add {} to distinguish them. Otherwise, the system will treat all the characters after $ as the variable name by default.

Who will show me how to use thinkphp's like to query databases?

Public function serCon () {$ search =$ _ GET ['wd ']; $ where ['title'] = array ('like', "% $ search % "); $ db = M ('themecards ')-> where ($ where)-> find (); print_r ($ db);} "% $ search %" single quotation marks are not allowed here, single quotes are used as strings and cannot be correctly parsed;
In the where condition, the entire $ where variable is required;
P function? Here, the returned value is of the array type and needs to be print_r. this can be ignored if it is a self-encapsulated print array function.


Examples in this article describe how thinkphp implements like fuzzy queries and share it with you for your reference. The specific implementation method is as follows :...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.