search
HomeBackend DevelopmentPHP Tutorialthinkphp implements like fuzzy query example, thinkphp fuzzy query_PHP tutorial

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:...
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment