db_query_range

Definition

db_query_range($query)
includes/database.pgsql.inc, line 183

Description

Runs a limited-range query in the active database.

Use this as a substitute for db_query() when a subset of the query is to be returned. User-supplied arguments to the query should be passed in as separate parameters so that they can be properly escaped to avoid SQL injection attacks.

Parameters

$query A string containing an SQL query.

... A variable number of arguments which are substituted into the query using printf) syntax. Instead of a variable number of query arguments, you may also pass a single array containing the query arguments.

$from The first result row to return.

$count The maximum number of result rows to return.

Return value

A database query result resource, or FALSE if the query was not executed correctly.

Related topics

Namesort iconDescription
Database abstraction layerAllow the use of different database servers using the same code base.

Code

<?php
function db_query_range($query) {
  $args = func_get_args();
  $count = array_pop($args);
  $from = array_pop($args);

  $query = db_prefix_tables($query);
  if (count(func_get_args()) > 3) {
    // Check for array (alternative syntax).
    if (is_array($args[1])) {
      $args = array_merge(array($query), $args[1]);
    }
    $args = array_map('db_escape_string', $args);
    $args[0] = $query;
    $query = call_user_func_array('sprintf', $args);
  }
  $query .= ' LIMIT '. (int)$count .' OFFSET '. (int)$from;
  return _db_query($query);
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.