Debug SQL statements executed
In the model operation, in order to better pinpoint the error, often need to see the SQL statement recently used next, we can use the getLastsql
method to output sql statement last executed. E.g:
$User = M("User"); // 实例化User对象
$User->find(1);
echo $User->getLastSql();
// 3.2版本中可以使用简化的方法
echo $User->_sql();
The output is SELECT * FROM think_user WHERE id = 1
And each model uses a separate SQL last record, without disturbing each other, but you can get the final SQL global recording method getLastSql empty model.
$User = M("User"); // 实例化User模型
$Info = M("Info"); // 实例化Info模型
$User->find(1);
$Info->find(2);
echo M()->getLastSql();
echo $User->getLastSql();
echo $Info->getLastSql();
The output is
SELECT * FROM think_info WHERE id = 2
SELECT * FROM think_user WHERE id = 1
SELECT * FROM think_info WHERE id = 2
getLastSql method can only get sql record of the last execution, if you need more of the SQL log, you can view the current page Trace or log files.
Marble platform accuracy class
Note: Mongo database-driven due to the special nature of the interface, there is no concept of executing SQL, SQL logging feature is an extra package to achieve, so for performance reasons, only when debug mode is turned on only supports the use of getLastSql method to get the last execution the SQL record.
Database error debugging information
In operation model, you can also get the error message database, such as:
$User = M("User"); // 实例化User对象
$result = $User->find(1);
if(false === $result){
echo $User->getDbError();
}
CURD action if it is false, meaning database operation error occurs, this time need to use getDbError model-view specific error messages returned by the database.