Home >PHP Framework >ThinkPHP >How to use the fetchSql method in ThinkPHP
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!
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.
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:
Results in sql tracking information:
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:
Results in sql tracking information:
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!