Skip to content

Commit

Permalink
Extended get_records so it can deal with LIMITs now
Browse files Browse the repository at this point in the history
  • Loading branch information
moodler committed Jul 31, 2003
1 parent 7e1a999 commit 0eeac48
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/datalib.php
Expand Up @@ -358,11 +358,12 @@ function get_record_select($table, $select="", $fields="*") {
}


function get_records($table, $field="", $value="", $sort="", $fields="*") {
function get_records($table, $field="", $value="", $sort="", $fields="*", $limitfrom="", $limitnum="") {
/// Get a number of records as an array of objects
/// Can optionally be sorted eg "time ASC" or "time DESC"
/// If "fields" is specified, only those fields are returned
/// The "key" is the first column returned, eg usually "id"
/// limitfrom and limitnum must both be specified or not at all

global $CFG;

Expand All @@ -372,11 +373,26 @@ function get_records($table, $field="", $value="", $sort="", $fields="*") {
$select = "";
}

if ($limitfrom) {
switch ($CFG->dbtype) {
case "mysql":
$limit = "LIMIT $limitfrom,$limitnum";
break;
case "postgres7":
$limit = "LIMIT $limitnum OFFSET $limitfrom";
break;
default:
$limit = "LIMIT $limitnum,$limitfrom";
}
} else {
$limit = "";
}

if ($sort) {
$sort = "ORDER BY $sort";
}

return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort");
return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort $limit");
}

function get_records_select($table, $select="", $sort="", $fields="*") {
Expand Down

0 comments on commit 0eeac48

Please sign in to comment.