A Brief Analysis of the Differences between execute and query Methods in ThinkPHP

  • 2021-06-29 10:40:49
  • OfStack

When first learning ThinkPHP, many people don't understand the difference between execute () and query () methods. This article will briefly analyze the difference between them.
It is well known that both the execute () and query () methods in ThinkPHP can enter SQL statements directly into the parameters.But the difference is that execute () is usually used to execute SQL statements such as insert or update, while query is often used to execute select and so on.
The execute () method will return the number of records affected, and if the select statement of SQL is executed, the result will be the total number of records in the table:
$model = M( "MyTable" );
$result = $model ->execute( 'update MyTable set name=aaa where id=11'); // Total rows will be returned 

The query() method returns the dataset:
$model = M( "MyTable" );
$result = $model ->query( 'select * from  MyTable' ); // Will return array()

Related articles: