search
HomePHP FrameworkThinkPHPHow to use the fetchSql method in ThinkPHP

How to use the fetchSql method in ThinkPHP

Mar 28, 2020 pm 05:08 PM
thinkphpuse

This article introduces the use of the fetchSql method in thinkphp. It has certain reference value. I hope it will be helpful to friends who are learning thinkphp!

How to use the fetchSql method in ThinkPHP

Usage of fetchSql method in ThinkPHP

Previously we learned a sql debugging method getLastSql method or alias _sql () method, but this method requires obtaining the last successfully executed sql statement, so if you use this method to debug sql, you can only debug logical errors, and cannot use it to debug syntax errors, so a new one was added after ThinkPHP 3.2.3 Method to debug sql: fetchSql();

Syntax:

$model -> where() -> limit() -> ...->order() -> fetchSql(true) ->CURD操作;

Note: The FetchSql method can be completely regarded as an auxiliary method when used, so it must be operated after the model and in the CURD Before, the order didn't matter. The FetchSql method can only be used after ThinkPHP3.2.3 version.

How to use the fetchSql method in ThinkPHP

The picture shows the manual of the version before ThinkPHP3.2.3

Go to the controller to test:

    //fetchSql方法
    public function test(){
        //实例化模型
        $model = M('Dept');
        //fetchSql方法
        $result = $model -> group('name') -> field('name,count(*)') -> fetchSql(true) -> select();
        //打印
        dump($result);
    }

Display the result:

How to use the fetchSql method in ThinkPHP

Results in sql tracking information:

How to use the fetchSql method in ThinkPHP

When the sql statement is wrong:

    //fetchSql方法
    public function test(){
        //实例化模型
        $model = M('Dept');
        //fetchSql方法
        $result = $model -> group('name') -> field('name,count(*,,,,,,,//)') -> fetchSql(true) -> select();
        //打印
        dump($result);
    }

Display results:

How to use the fetchSql method in ThinkPHP

Results in sql tracking information:

How to use the fetchSql method in ThinkPHP

Description: Through tracking information and return values, we can find, After using fetchSql, the original coherent operation is not executed, but the sql statement composed of the syntax of the coherent operation is directly returned.

(Recommended tutorial: thinkphp tutorial)

The above is the detailed content of How to use the fetchSql method in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.