Home >PHP Framework >ThinkPHP >How to use the fetchSql method in ThinkPHP

How to use the fetchSql method in ThinkPHP

angryTom
angryTomforward
2020-03-28 17:08:433327browse

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.net. If there is any infringement, please contact admin@php.cn delete