Detailed Explanation of limit Method of ThinkPHP CURD Method

  • 2021-07-01 06:54:18
  • OfStack

limit method of ThinkPHP CURD method is also one of the coherent operation methods of model class, which is mainly used to specify the number of queries and operations, especially when paging queries. And the limit method of ThinkPHP can be compatible with all database-driven classes.

Its specific usage is as follows:

1. Limit the number of results:

For example, to get 10 users who meet the requirements, you can call as follows:


$User = M('User');
$User->where('status=1')->field('id,name')->limit(10)->select();

The limit method can also be used for write operations, such as updating three pieces of data that meet the requirements:


$User = M('User');
$User->where('score=100')->limit(3)->save(array('level'=>'A'));

2. Paged queries:

Used for article paging query is a common occasion for limit method, such as:


$Article = M('Article');
$Article->limit('10,25')->select();

Represents querying article data, 25 pieces of data starting at line 10 (possibly also depending on the impact of where conditions and order sorting, not to mention this).
After version 3.1, you can also use it like this:


$Article = M('Article');
$Article->limit(10,25)->select();

In addition, for large data tables, try to use limit to limit query results, otherwise it will lead to large memory overhead and performance problems.


Related articles: