thinkphp implement like Fuzzy query instance, thinkphp Fuzzy query _php tutorial

Source: Internet
Author: User

thinkphp implement like Fuzzy query instance, thinkphp fuzzy query


The example of this paper describes the thinkphp implementation of like fuzzy Query method, share for everyone for reference. The implementation method is as follows:

At present, there are more and more people using thinkphp framework for project development, because of its good encapsulation, which leads to a lot of pure PHP development, the example of this paper is illustrated with like Fuzzy query.

Here are the main examples of usage:

Thinkphp can support direct use of strings as query criteria, but most of the cases recommend using indexed arrays or objects as query criteria because they are more secure.

I. Using strings as query criteria

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

Copy the Code code as follows: $User = M ("User"); Instantiating a User object
$User->where (' type=1 and Status=1 ')->select ();
The last SQL statement generated is

Copy the Code code as follows: SELECT * from Think_user WHERE type=1 and Status=1

If you make a multi-field query, the default logical relationship between fields is logical and and, but with the following rules you can change the default logical judgments by using _logic to define the query logic:

Copy the Code code as follows: $User = M ("User"); Instantiating a User object
$condition [' name '] = ' thinkphp ';
$condition [' account '] = ' thinkphp ';
$condition [' _logic '] = ' OR ';
Passing query criteria into Query method
$User->where ($condition)->select ();
The last SQL statement generated is

Copy the code as follows: SELECT * from Think_user WHERE ' name ' = ' thinkphp ' OR ' account ' = ' thinkphp '

Second, array mode as a query condition

So much has been said about how the like query is implemented, see below

Copy the Code code as follows: $userForm =m (' user ');
$where [' Name ']=array (' like ', ' jb51% ');
$userForm->where ($where)->select ();
Here is the like query: copy Code code as follows: Name "jb51%"

Query statement: Copy Code code as follows: $where [' Name ']=array (' like ', Array ('%jb51% ', '%.com '), ' OR ');
Here is the like query: copy Code code as follows: Name "%jb51%" or name "%.com"

Query statement: Copy Code code as follows: $where [' Name ']=array (' like ', '%a% '), array (' Like ', '%b% '), array (' Like ', '%c% '), ' jb51 ' , ' or ');
The like query here is the following: copy code code as follows: (' name ' as '%a% ') or (' name ' like '%b% ') or (' name ' as '%c% ') or (' name ' = ' jb51 ')

Query statement: Copy Code code as follows: $where [' _string ']= ' (name like "%jb51%") OR (title "%jb51") ';
Here's the like query: Copy the Code code as follows: Name "%jb51%" or "%jb51"

I hope this article is helpful to everyone's PHP programming.


Zhao Tun is talking about thinkphp in SQL Fuzzy Query%{$_post[' username ']}%, why is there {}?

If there are characters before and after the variable, you should add {} to differentiate it, otherwise the system will default to all the characters after all as the name of the variable.

Who show me I use thinkphp to implement like query database, how not to work, where wrong

Public Function Sercon () {$search = $_get[' wd '); $where [' title '] = Array (' Like ', '% $search% '); $db = M (' themecards ')->w Here ($where)->find (); Print_r ($DB); } "% $search%" can not be used in single quotes, single quotes words variables will be treated as strings, can not parse correctly;
In the Where condition, as said downstairs, the entire $where variable is required;
P function? The return value here is an array type, which needs to be print_r, which can be omitted if it is a function of the printed array of its own encapsulation.


http://www.bkjia.com/PHPjc/902781.html www.bkjia.com true http://www.bkjia.com/PHPjc/902781.html techarticle thinkphp implement like fuzzy query instance, thinkphp Fuzzy query This paper describes thinkphp implementation of like fuzzy Query method, share to everyone for your reference. The concrete 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.