thinkphp实现like模糊查询实例,thinkphp模糊查询
thinkphp实现like模糊查询实例,thinkphp模糊查询
本文实例讲述了thinkphp实现like模糊查询的方法,分享给大家供大家参考。具体实现方法如下:
目前使用thinkphp框架进行项目开发的人越来越多了,由于其封装性较好,导致了很多纯PHP开发的部分不易上手,本文实例即以like模糊查询为例对此加以说明。
这里主要通过举例来说明用法:
ThinkPHP可以支持直接使用字符串作为查询条件,但是大多数情况推荐使用索引数组或者对象来作为查询条件,因为会更加安全。
一、使用字符串作为查询条件
这是最传统的方式,但是安全性不高,
例如:
复制代码 代码如下:
$User = M("User"); // 实例化User对象
$User->where('type=1 AND status=1')->select();
最后生成的SQL语句是
复制代码 代码如下:
SELECT * FROM think_user WHERE type=1 AND status=1
如果进行多字段查询,那么字段之间的默认逻辑关系是 逻辑与 AND,但是用下面的规则可以更改默认的逻辑判断,通过使用 _logic 定义查询逻辑:
复制代码 代码如下:
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// 把查询条件传入查询方法
$User->where($condition)->select();
最后生成的SQL语句是
复制代码 代码如下:
SELECT * FROM think_user WHERE `name`='thinkphp' OR `account`='thinkphp'
二、数组方式作为查询条件
讲了这么多了like查询怎么实现呢,下面看
复制代码 代码如下:
$userForm=M('user');
$where['name']=array('like','jb51%');
$userForm->where($where)->select();
这里的like查询即为:
复制代码 代码如下:
name like 'jb51%'
查询语句:
复制代码 代码如下:
$where['name']=array('like',array('%jb51%','%.com'),'OR');
这里的like查询即为:
复制代码 代码如下:
name like '%jb51%' or name like '%.com'
查询语句:
复制代码 代码如下:
$where['name']=array(array('like','%a%'),array('like','%b%'),array('like','%c%'),'jb51','or');
这里的like查询即为:
复制代码 代码如下:
(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'jb51')
查询语句:
复制代码 代码如下:
$where['_string']='(name like "%jb51%") OR (title like "%jb51")';
这里的like查询即为:
复制代码 代码如下:
name like '%jb51%' or title like '%jb51'
希望本文所述对大家的PHP程序设计有所帮助。
变量前后有字符的话,要加上 {} 来区分,不然系统会默认的将$ 后面所有的字符都当成变量的名字
public function serCon() { $search = $_GET['wd']; $where['title'] = array('like',"%$search%"); $db = M('ThemeCards')->where($where)->find(); print_r($db); }"%$search%"这里不能用单引号,单引号的话变量会被当成字符串,不能正确解析;
where条件中如楼下所说,需要整个$where变量;
p函数?这里返回值是数组类型,需要print_r,如果是自己封装的打印数组的函数的话这个可以忽略。

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

AI Hentai Generator
Generate AI Hentai for free.

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



Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same
