The ThinkPHP framework gets a simple example of the last SQL statement execution and variable debugging

  • 2021-10-16 01:15:42
  • OfStack

This article example tells the ThinkPHP framework to obtain the last time to execute SQL statements and variable debugging simple operations. Share it for your reference, as follows:

There are two ways to get the last sql statement executed in ThinkPHP:

Its 1 is to call the model to get such as:


$sql = $model ->getLastSql();

The Model class in Thinkphp includes the function getLastSql, and even the functions getLastInsID, getDbError, getError, getPk, getDbFields, etc. These functions are all functions of model layer that we may often use.

Its second is in version 3.2. 3, adding fetchSql () function.

Such as


$sql= $model->fetchSql(true)->add($data);

A simplified approach can be used in version 3.2:


echo $model->_sql();

Variable debugging

The ThinkPHP framework has a built-in browser-friendly dump method, which is used to output variable information to the browser for viewing.

Usage: dump($var, $echo=true, $label=null, $strict=true)


$Blog = D("Blog");
$blog = $Blog->find(3);
dump($blog);

You can use the E method to output an error message and interrupt execution, for example:


// Output error message and abort execution 
E($msg);

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: