Home >PHP Framework >YII >How to print sql statement in yii2?
yii2 How to print sql statements: You can execute the statement in the controller, the code is [$model->find()->createCommand()->getRawSql();], click on the web page You can see the sql statement in the log at the bottom.
yii2 method of printing sql statement:
If you want to print this Sql, you can use
$model->find()->createCommand()->getRawSql();
You can see it in the debugger at the bottom of the web page. Click on the log to see the sql language you executed (that is, the sql statement for consecutive operations);
For example, in the controller, you execute
$query= ReleaseForm::find()->where(['type'=>1])->all();
Note: The source code is these:
$query= ReleaseForm::find()->where(['type'=>1]); $pagination = new Pagination([ 'defaultPageSize' => 5, 'totalCount' => $query->count(), ]); $property_data= $query ->offset($pagination->offset) ->limit($pagination->limit) ->all();
In the web page, you can click on the log at the bottom to see:
10 21: 01:36.667 info yii\db\Command::query SELECT COUNT(*) FROM `releases` WHERE `type`=1
E:\web\Apache24\htdocs\basic\controllers\PropertyController.php (14)
11 21:01:36.667 info yii\db\Connection::open Opening DB connection: mysql:host=localhost;dbname=xunwu
E:\web\Apache24\htdocs\basic\controllers\PropertyController.php ( 14)
12 21:01:36.669 info yii\db\Command::query SELECT * FROM `releases` WHERE `type`=1 LIMIT 5
E:\web\Apache24\htdocs\basic\controllers\ PropertyController.php (20)
13 21:01:36.670 info yii\db\Command::query SHOW FULL COLUMNS FROM `releases`
E:\web\Apache24\htdocs\basic\controllers\PropertyController.php (20)
14 21:01:36.674 info yii\db\Command::query SHOW CREATE TABLE `releases`
This way you will know what sql statement you executed
Related learning recommendations: yii tutorial
The above is the detailed content of How to print sql statement in yii2?. For more information, please follow other related articles on the PHP Chinese website!