| 7 database.inc | db_query_range($query, |
| 4.6 database.pgsql.inc | db_query_range($query) |
| 4.6 database.mysql.inc | db_query_range($query) |
| 4.7 database.pgsql.inc | db_query_range($query) |
| 4.7 database.mysqli.inc | db_query_range($query) |
| 4.7 database.mysql.inc | db_query_range($query) |
| 5 database.pgsql.inc | db_query_range($query) |
| 5 database.mysqli.inc | db_query_range($query) |
| 5 database.mysql.inc | db_query_range($query) |
| 6 database.pgsql.inc | db_query_range($query) |
| 6 database.mysqli.inc | db_query_range($query) |
| 6 database.mysql.inc | db_query_range($query) |
| 8 database.inc | db_query_range($query, $from, $count, array $args = array(), array $options = array()) |
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. Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose in '') and %%.
NOTE: using this syntax will cast NULL and FALSE values to decimal 0, and TRUE values to decimal 1.
$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
- aggregator_block in modules/
aggregator/ aggregator.module - Implementation of hook_block().
- aggregator_page_categories in modules/
aggregator/ aggregator.pages.inc - Menu callback; displays all the categories used by the aggregator.
- aggregator_page_rss in modules/
aggregator/ aggregator.pages.inc - Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
- aggregator_page_sources in modules/
aggregator/ aggregator.pages.inc - Menu callback; displays all the feeds used by the aggregator.
- blogapi_blogger_get_recent_posts in modules/
blogapi/ blogapi.module - Blogging API callback. Returns the latest few postings in a user's blog. $bodies TRUE <a href="http://movabletype.org/docs/mtmanual_programmatic.html#item_mt%2EgetRece... returns a bandwidth-friendly list</a>.
- drupal_error_handler in includes/
common.inc - Log errors as defined by administrator.
File
- includes/
database.pgsql.inc, line 271 - Database interface code for PostgreSQL database servers.
Code
function db_query_range($query) {
$args = func_get_args();
$count = array_pop($args);
$from = array_pop($args);
array_shift($args);
$query = db_prefix_tables($query);
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}
_db_query_callback($args, TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
$query .= ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from;
return _db_query($query);
}
Comments
How use this function in Drupal 6
PermalinkPlease note the order of arguments in Drupal 6 is switched around from what it is in Drupal 7
db_query_range($query, $args,$from, $count);
Enjoy it.
Looks like in Drupal 7 this
PermalinkLooks like in Drupal 7 this should have arguments as:
db_query_range($query, $from, $count, $args, $options);
See:
http://api.drupal.org/api/drupal/includes%21database%21database.inc/func...
$from parameter
PermalinkQuote from MySQL documentation: "The offset of the initial row is 0 (not 1)", so use 0 in $from if you would like to retrieve from the first record.
Sample example query
PermalinkWe can use db_query_range() for retrieve limited records
db_query_range("SELECT nid FROM {node}", 0, 6);
Instead of
db_query("SELECT nid FROM {node} limit 0, 6");
A example using all parameters
PermalinkThank you for offering an example. However, in the interest of illustrating the actual underlying API, the second parameter to db_query_range() should be the argument list for the query. In the case of your example, it would be:
db_query_range("SELECT nid FROM {node}", array(), 0, 6);Or, to use an example with an actual argument list:
// Select only the first result by using LIMIT = 1db_query_range("SELECT nid FROM {node} WHERE title LIKE '%s%%' and created > %d", array($title_fragment, $timestamp), 0, 1);