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:
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:
2. Array method as query condition
Having talked so much about how to implement like query, let’s see below
Query statement:
Query statement:
Query statement:
I hope this article will be helpful to everyone’s PHP programming design.
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
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.