Community Documentation

Ranges and limits

Last updated November 26, 2012. Created by drupalshrek on November 23, 2012.
Log in to edit this page.

Ranges and Limits

Queries may also be restricted to a certain subset of the records found. In general this is known as a "range query". In MySQL, this is implemented using the LIMIT clause. To limit the range of a query, use the range() method.

In most cases we want "the first n records". To do that, pass 0 as the first argument and n as the second.

<?php
// Limit the result to 10 records
$query->range(0, 10);
?>

The following example will instruct the result set to start at the 6th record found (the count starts at 0) rather than the first, and to return only 10 records.

<?php
$query
->range(5, 10);
?>

Calling the range() method a second time will overwrite previous values. Calling it with no parameters will remove all range restrictions on the query.

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate, Advanced
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.