Home > PHP Framework > ThinkPHP > body text

How to use the fetchSql method in ThinkPHP

angryTom
Release: 2020-03-28 17:08:43
forward
3295 people have browsed it

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操作;
Copy after login

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);
    }
Copy after login

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);
    }
Copy after login

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template