Home Backend Development PHP Tutorial ThinkPHP query statements and usage of related queries

ThinkPHP query statements and usage of related queries

Jun 08, 2018 am 10:44 AM
thinkphp Related query Inquire statement

This article mainly introduces the usage of ThinkPHP query statements and related queries, and common query methods in the form of examples, including techniques such as arrays as query conditions and object methods for querying. Friends in need can refer to it

The examples in this article describe the usage of ThinkPHP query statements and related queries. Share it with everyone for your reference. The details are as follows:

In the thinkphp framework page, we can directly spell sql query statements to implement database query read and write operations. Here is an example to illustrate this.

In addition to string query conditions for ordinary queries, array and object query conditions are very commonly used, and these are necessary for basic queries.

1. Use arrays as query conditions

$User = M("User"); //实例化User对象
$condition['name'] = 'thinkphp'; // 把查询条件传入查询方法
$User->where($condition)->select();
Copy after login

2. Use object mode to query. You can use any object. Here we take the stdClass built-in object as an example.

$User = M("User"); // 实例化User对象
// 定义查询条件 $condition = new stdClass();
$condition->name = 'thinkphp';  // 查询name的值为thinkphp的记录
$User->where($condition)->select(); //  上面的查询条件等同于 where('name="thinkphp"') 使用对象方式查询和使用数组查询的效果是相同的,并且是可
带where条件的普通查询
Copy after login

1. String form

$user=M('user');
$list=$user->where(&#39;id>5 and id<9&#39;)->select();
$list=$user->where($data)->select();
Copy after login

2. Array format

$user=M(&#39;user&#39;);
$list=$user->where(array(&#39;username&#39;=>&#39;www.jb51.net&#39;))->select();
$list=$user->where($data)->select();
Copy after login

3. Object format

$user=M(&#39;user&#39;);
$a=new stdClass();
$a->username=&#39;www.jb51.net;
$list=$user->where($a)->select();
Copy after login

Associative query between two tables:

$M_shopping = M(&#39;Shops&#39;); 
$M_product = M(&#39;Product&#39;); 
$list_shops = $M_shopping->join(&#39;as shops left join hr_product as product on shops.product_id = product.p_id&#39;) 
->field(&#39;product.p_id,product.p_name,shops.product_amount,shops.product_id&#39;) 
->where("shops.user_cookie=&#39;".$_COOKIE[&#39;hr_think_userid&#39;]."&#39;") 
->group(&#39;shops.id&#39;) 
->select();
Copy after login

Interval query

$user=M(&#39;user&#39;);
$data[&#39;id&#39;]=array(array(&#39;gt&#39;,20),array(&#39;lt&#39;,23),&#39;and&#39;);
$list=$user->where($data)->select();
Copy after login

Combined query

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=&#39;pengyanjie&#39;;
$data[&#39;password&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;id&#39;]=array(&#39;lt&#39;,30);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$list=$user->where($data)->select();
dump($list);
Copy after login

Compound query

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;password&#39;]=array(&#39;like&#39;,&#39;p%&#39;);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$where[&#39;_complex&#39;]=$where;
$where[&#39;id&#39;]=array(&#39;lt&#39;,30);
$list=$user->where($data)->select();
Copy after login

Associated query of three data tables

$M_shopping = M(&#39;Shops&#39;);
$M_product = M(&#39;Product&#39;);
$M_proimg = M(&#39;Product_image&#39;);
$list_shops = $M_shopping->join(&#39;as shops left join hr_product as product on shops.product_id = product.p_id left join
hr_product_image as productimgon productimg.p_id = product.p_id&#39;)->fiel(&#39;productimg.pi_url,product.p_id,product.p_name,shops.product_amount,shops.product_id,product.am_id,
product.p_procolor,product.p_price,product_amount*p_price as totalone&#39;)->where("shops.user_cookie=&#39;".$_COOKIE[&#39;hr_think_userid&#39;]."&#39;")
->group(&#39;shops.id&#39;)->select();
Copy after login

Query conditions of the data table
① The following is a direct query The conditions are placed in where, which makes it easier to write the conditions

$m_test = M("Product");
$productmeaage = $m_test->where("p_id=&#39;$proid&#39;")->select();
Copy after login

② In addition to the above method, there is also an array method

$M_product = M(&#39;Product&#39;);
$map[&#39;pid&#39;] = $proid;
$p_result = $M_product->where($map)->select();
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

ThinkPHP’s association model

##Query language commonly used in ThinkPHP

The above is the detailed content of ThinkPHP query statements and usage of related queries. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

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.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

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.

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

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

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

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];

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

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.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

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.

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

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

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

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.

See all articles