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

thinkphp implements like fuzzy query example, thinkphp fuzzy query_PHP tutorial

WBOY
Release: 2016-07-13 10:15:47
Original
1410 people have browsed it

thinkphp implements like fuzzy query example, thinkphp fuzzy query

The example in this article describes how thinkphp implements like fuzzy query and is shared 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. This article uses like fuzzy query as an example to illustrate this.

Here are mainly 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 is 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"); // Instantiate User object
$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, the default logical relationship between fields is logical AND, but you can change the default logical judgment using the following rules, by using _logic to define the query logic:

Copy code The code is as follows:
$User = M("User"); // Instantiate User object
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// Pass the query conditions into the query method
$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 method as query condition

Having talked so much about how to implement like query, let’s see below

Copy code The code is as follows:
$userForm=M('user');
$where['name']=array('like','jb51%');
$userForm->where($where)->select();

The like query here is:
Copy code The code is as follows:
name like 'jb51%'

Query statement:

Copy code The code is as follows:
$where['name']=array('like',array('% jb51%','%.com'),'OR');

The like query here is:
Copy code The code is as follows:
name like '%jb51%' 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%'),'jb51','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` = 'jb51')

Query statement:

Copy code The code is as follows:
$where['_string']='(name like "%jb51%") OR (title like "%jb51")';

The like query here is:
Copy code The code is as follows:
name like '%jb51%' or title like '%jb51'

I hope this article will be helpful to everyone’s PHP programming design.

When Zhao Tongzheng talks about SQL fuzzy query in thinkphp %{$_POST['username']}%, why is there {}?

If there are characters before and after the variable, {} must be added to distinguish it, otherwise the system will default to treating all characters after $ as the name of the variable

Who can show me how I use thinkphp to implement like to query the database, why it doesn’t work, what went wrong

public function serCon() { $search = $_GET['wd']; $where['title'] = array('like',"%$search%"); $db = M('ThemeCards')-> ;where($where)->find(); print_r($db); }"%$search%" Single quotes cannot be used here. If single quotes are used, the variable will be treated as a string and cannot be parsed correctly;
where As mentioned below, the condition requires the entire $where variable;
p function? The return value here is an array type and print_r is required. If it is a self-encapsulated function that prints an array, this can be ignored.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/902781.htmlTechArticlethinkphp implements like fuzzy query examples, thinkphp fuzzy query This example describes how thinkphp implements like fuzzy query, share it with For your reference. The specific implementation method is as follows:...
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