Home Backend Development PHP Tutorial How to use native MySQL statements in TP (code)

How to use native MySQL statements in TP (code)

Aug 20, 2018 pm 05:22 PM
tp

The content of this article is about the method (code) of using native MySQL statements in TP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Recently I was developing a project in Thinkphp and wanted to use native MySQL statements. I finally tried it and found it particularly helpful for complex query and update operations! Let’s introduce it in detail below!

1. Use the native mysql core

$Model = M();        
$result = $Model->query($sql);//查询
$Model->execute($sql_ex);//更新修改删除
Copy after login

2. Specific usage

  <?php
    /**
     * 一亩地
     */
    public function dealYimudi(){
        $sql = "SELECT * from yimudi y LEFT JOIN yimudi_use yu on y.yimudi_id =yu.yimudi_id WHERE  yu.yimudi_id = 12 and yu.use_status in (1,2) and y.use_end_time+86399 < UNIX_TIMESTAMP(now())";

        $Model = M();
        $result = $Model->query($sql);

        $yimudi_ids =[];
        foreach ($result as $key => $value) {
            $yimudi_ids[] =$value[&#39;id&#39;];
        }
        foreach ($result as $key1 => $value1) {
           if(in_array($value1[&#39;id&#39;],$yimudi_ids)){
            $str_yimudi_id = $value1[&#39;id&#39;];
            $sql_ex = "update yimudi_use set use_status = 6 where id = &#39;$str_yimudi_id&#39;";

              $Model->execute($sql_ex);

           }
        }
        // $result = $Model->query($sql);
        // dump($result);die;
    }
Copy after login

You can try it!

Related recommendations:

Thinkphp upload class implements code for uploading images

How to use TP5.1 template loop tags (code)

The above is the detailed content of How to use native MySQL statements in TP (code). 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)

Performance optimization and debugging of TP6 Think-Swoole RPC service Performance optimization and debugging of TP6 Think-Swoole RPC service Oct 12, 2023 am 11:16 AM

Performance optimization and debugging of TP6Think-SwooleRPC service 1. Introduction With the rapid development of the Internet, distributed computing has become an indispensable part of modern software development. In distributed computing, RPC (RemoteProcedureCall, Remote Procedure Call) is a commonly used communication mechanism through which method calls across the network can be implemented. Think-Swoole, as a high-performance PHP framework, can support RPC services well. but

High scalability and distributed deployment of TP6 Think-Swoole RPC service High scalability and distributed deployment of TP6 Think-Swoole RPC service Oct 12, 2023 am 11:07 AM

TP6 (ThinkPHP6) is an open source framework based on PHP, which has the characteristics of high scalability and distributed deployment. This article will introduce how to use TP6 with Swoole extension to build a highly scalable RPC service, and give specific code examples. First, we need to install TP6 and Swoole extensions. Execute the following command in the command line: composerrequiretopthink/thinkpeclinstallswo

Data encryption and identity authentication mechanism of TP6 Think-Swoole RPC service Data encryption and identity authentication mechanism of TP6 Think-Swoole RPC service Oct 12, 2023 am 11:29 AM

Data encryption and identity authentication mechanism of TP6Think-SwooleRPC service With the rapid development of the Internet, more and more applications need to make remote calls to realize data interaction and function calls between different modules. In this context, RPC (RemoteProcedureCall) has become an important communication method. The TP6Think-Swoole framework can implement high-performance RPC services. This article will introduce how to use data encryption and identity authentication.

TP6 Think-Swoole's RPC service and message queue integration and application TP6 Think-Swoole's RPC service and message queue integration and application Oct 12, 2023 am 11:37 AM

Integration and application of TP6Think-Swoole's RPC service and message queue In modern software development, RPC service (RemoteProcedureCall) and message queue are common technical means used to implement service calls and asynchronous message processing in distributed systems. Integrating Think-Swoole components in the TP6 framework can easily implement the functions of RPC services and message queues, and provides concise code examples for developers to understand and apply. 1. RPC

Highly concurrent request processing and scheduling of TP6 Think-Swoole RPC service Highly concurrent request processing and scheduling of TP6 Think-Swoole RPC service Oct 12, 2023 pm 12:33 PM

Highly concurrent request processing and scheduling of TP6Think-SwooleRPC service With the continuous development of Internet technology, concurrent request processing and scheduling of network applications has become an important challenge. In the TP6 framework, the Think-Swoole extension can be used to implement high-concurrency request processing and scheduling of the RPC (RemoteProcedureCall) service. This article will introduce how to build a Think-Swoole-based RPC service in the TP6 framework and provide

Security protection and authorization verification of TP6 Think-Swoole RPC service Security protection and authorization verification of TP6 Think-Swoole RPC service Oct 12, 2023 pm 01:15 PM

Security protection and authorization verification of TP6Think-SwooleRPC service With the rise of cloud computing and microservices, remote procedure call (RPC) has become an essential part of developers' daily work. When developing RPC services, security protection and authorization verification are very important to ensure that only legitimate requests can access and call the service. This article will introduce how to implement security protection and authorization verification of RPC services in the TP6Think-Swoole framework. 1. Basic concepts of RPC services

Learn how to count the total amount of data in PHP TP5 Learn how to count the total amount of data in PHP TP5 Mar 24, 2024 pm 06:39 PM

To learn how PHPTP5 counts the total amount of data, you need specific code examples. When using PHP to develop websites, counting the total amount of data is a very common requirement. In the TP5 framework, we can implement statistics on the total amount of data through SQL statements or methods provided by the framework. This article will introduce how to count the total amount of data in the TP5 framework and provide specific code examples. 1. Use SQL statements to count the total amount of data. In the TP5 framework, we can use SQL statements to count the total amount of data. For example, if we have a

High-performance database access optimization strategy for TP6 Think-Swoole RPC service High-performance database access optimization strategy for TP6 Think-Swoole RPC service Oct 12, 2023 pm 01:27 PM

High-performance database access optimization strategy for TP6Think-SwooleRPC service Introduction: With the rapid development of Internet technology, more and more applications require high-performance database access capabilities. In the TP6Think-Swoole framework, RPC service is one of the important components to achieve high-performance database access. This article will introduce some optimization strategies to improve the database access performance of the TP6Think-SwooleRPC service and give some specific code examples. one

See all articles