8.3.x database.inc db_query_range($query, $from, $count, array $args = [], array $options = [])
8.0.x database.inc db_query_range($query, $from, $count, array $args = array(), array $options = array())
8.1.x database.inc db_query_range($query, $from, $count, array $args = array(), array $options = array())
8.2.x database.inc db_query_range($query, $from, $count, array $args = array(), array $options = array())
8.4.x database.inc db_query_range($query, $from, $count, array $args = [], array $options = [])
4.6.x database.pgsql.inc db_query_range($query)
4.6.x database.mysql.inc db_query_range($query)
4.7.x database.pgsql.inc db_query_range($query)
4.7.x database.mysqli.inc db_query_range($query)
4.7.x database.mysql.inc db_query_range($query)
5.x database.pgsql.inc db_query_range($query)
5.x database.mysqli.inc db_query_range($query)
5.x database.mysql.inc db_query_range($query)
6.x database.pgsql.inc db_query_range($query)
6.x database.mysqli.inc db_query_range($query)
6.x database.mysql.inc db_query_range($query)
7.x database.inc db_query_range($query, $from, $count, array $args = array(), array $options = array())

Executes a query against the active database, restricted to a range.


$query: The prepared statement query to run. Although it will accept both named and unnamed placeholders, named placeholders are strongly preferred as they are more self-documenting.

$from: The first record from the result set to return.

$count: The number of records to return from the result set.

$args: An array of values to substitute into the query. If the query uses named placeholders, this is an associative array in any order. If the query uses unnamed placeholders (?), this is an indexed array and the order must match the order of placeholders in the query string.

$options: An array of options to control how the query operates.

Return value

DatabaseStatementInterface A prepared statement object, already executed.

See also


Related topics

56 calls to db_query_range()
AggregatorTestCase::getDefaultFeedItemCount in modules/aggregator/aggregator.test
Returns the count of the randomly created feed array.
aggregator_block_view in modules/aggregator/aggregator.module
Implements hook_block_view().
aggregator_page_categories in modules/aggregator/aggregator.pages.inc
Page callback: Displays all the categories used by the Aggregator module.
aggregator_page_rss in modules/aggregator/aggregator.pages.inc
Page callback: Generates an RSS 0.92 feed of aggregator items or categories.
aggregator_page_sources in modules/aggregator/aggregator.pages.inc
Page callback: Displays all the feeds used by the aggregator.

... See full list

1 string reference to 'db_query_range'
_drupal_decode_exception in includes/errors.inc
Decodes an exception and retrieves the correct caller.


includes/database/database.inc, line 2433
Core systems for the database layer.


function db_query_range($query, $from, $count, array $args = array(), array $options = array()) {
  if (empty($options['target'])) {
    $options['target'] = 'default';

  return Database::getConnection($options['target'])->queryRange($query, $from, $count, $args, $options);


aaronbauman’s picture

If you have a particularly large dataset, processing records in chunks with db_query_range() can cut down resource usage and prevent memory or time limit errors.

$n = 2000; // Set appropriate chunk size for your dataset.
$position = 0;
do {
  $items = array();
  $results = db_query_range($sql, ($position++ * $n), $n, $args)->fetchAll();
  if (empty($results)) {
    break; // Querying complete, or a query error.
  // ... do your processing here
} while (TRUE);
drupalshrek’s picture

The $from parameter starts from 0 (not 1), so for example, the following selects the first record (i.e. $from = 0 and $count = 1

  $result = db_query_range($sql, 0, 1);