Related knowledge about php+mysql fuzzy query function
php MySQL fuzzy query function is very important for PHP to operate the database. This article will explain its related knowledge in detail.
General fuzzy query statements are as follows:
SELECT field FROM table WHERE certain field Like condition
Regarding conditions, SQL provides four matching modes:
1, %: represents any 0 or more characters. Can match characters of any type and length. In some cases, if it is Chinese, please use two percent signs (%%) to express it.
For example, SELECT * FROM [user] WHERE u_name LIKE '%三%'
will treat u_name as "Zhang San", "Zhang Cat San", "Three-legged Cat", "Tang Sanzang" and so on. Find out all the records of "three".
In addition, if you need to find records with both "三" and "cat" in u_name, please use the and condition
SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '% Cat%'
If you use SELECT * FROM [user] WHERE u_name LIKE '%三%cat%'
Although you can search for "three-legged cat", you cannot search for "Zhang Cat San" that meets the conditions.
2, _: represents any single character. Matches a single arbitrary character. It is often used to limit the character length of expression statements:
For example, SELECT * FROM [user] WHERE u_name LIKE '_三_'
Only find " "Tang Sanzang" such that u_name has three characters and the middle character is "三";
Another example is SELECT * FROM [user] WHERE u_name LIKE '三';
Only find "three-legged cat" In this way, the name has three characters and the first character is "三";
3, [ ]: represents one of the characters listed in brackets (similar to regular expression). Specify a character, string or range, and require the matched object to be any one of them.
For example, SELECT * FROM [user] WHERE u_name LIKE '[Zhang Li Wang] San'
will find "Zhang San", "Li San", "Wang San" (instead of "Zhang Li Wang] Wang San");
If [ ] contains a series of characters (01234, abcde, etc.), it can be abbreviated as "0-4", "a-e"
SELECT * FROM [user] WHERE u_name LIKE 'Old [1-9]'
will find "Old 1", "Old 2",..., "Old 9";
4, [^]: means not in the brackets A single character within a column. Its value is the same as [], but it requires that the matched object is any character other than the specified character.
For example, SELECT * FROM [user] WHERE u_name LIKE '[^Zhang Li Wang] San'
will find "Zhao San", "Sun San" etc.;
SELECT * FROM [user] WHERE u_name LIKE '老[^1-4]';
will exclude "Old 1" to "Old 4" and search for "Old 5" ", "Old 6",...
5, when the query content contains wildcard character ,
due to the wildcard character, we query special characters Statements such as "%", "_", and "[" cannot be implemented normally, but special characters can be queried normally if they are enclosed in "[ ]". Based on this, we wrote the following function:
function sqlencode(str) str=replace(str,"[","[[]") '此句一定要在最前 str=replace(str,"_","[_]") str=replace(str,"%","[%]") sqlencode=str end function
This article explains the fuzzy query function of php mysql. For more learning materials, please pay attention to the php Chinese website.
Related recommendations:
How to copy and move files through php
About jQuery effects - related to hiding and displaying Knowledge
Related knowledge points about SQL NULL values
The above is the detailed content of Related knowledge about php+mysql fuzzy query function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

MySQL uses GPL and commercial licenses for small and open source projects; Oracle uses commercial licenses for enterprises that require high performance. MySQL's GPL license is free, and commercial licenses require payment; Oracle license fees are calculated based on processors or users, and the cost is relatively high.

MySQL is a database management system, and phpMyAdmin is a web tool for managing MySQL. 1.MySQL is used to store and manage data and supports SQL operations. 2.phpMyAdmin provides a graphical interface to simplify database management.

Navicat and MySQL are perfect matches because they can improve database management and development efficiency. 1.Navicat simplifies MySQL operations and improves work efficiency through graphical interfaces and automatic generation of SQL statements. 2.Navicat supports multiple connection methods, which facilitates local and remote management. 3. It provides powerful data migration and synchronization capabilities, suitable for advanced usage. 4.Navicat helps with performance optimization and best practices such as regular backup and query optimization.

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

MySQL is popular because of its excellent performance and ease of use and maintenance. 1. Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2. Insert and query data: operate data through INSERTINTO and SELECT statements. 3. Optimize query: Use indexes and EXPLAIN statements to improve performance.
