thinkphp model debugging

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 getLastsqlmethod to output sql statement last executed. E.g:

  1. $User = M("User"); // 实例化User对象
  2. $User->find(1);
  3. echo $User->getLastSql();
  4. // 3.2版本中可以使用简化的方法
  5. 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.

  1. $User = M("User"); // 实例化User模型
  2. $Info = M("Info"); // 实例化Info模型
  3. $User->find(1);
  4. $Info->find(2);
  5. echo M()->getLastSql();
  6. echo $User->getLastSql();
  7. echo $Info->getLastSql();

The output is

  1. SELECT * FROM think_info WHERE id = 2
  2. SELECT * FROM think_user WHERE id = 1
  3. 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:

  1. $User = M("User"); // 实例化User对象
  2. $result = $User->find(1);
  3. if(false === $result){
  4. echo $User->getDbError();
  5. }

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.

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11834849.html