diff --git a/CHANGELOG b/CHANGELOG
index 1f45b4f8f74d8c45100db3023f163aaa145fec39..3474b769a27e4f0e27d09a4d721a0b19fcbab841 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,25 +1,19 @@
 Drupal x.x.x, xxxx-xx-xx (to be released)
 ------------------------
 
-- clean urls.
+- clean URLs.
 - reorganized the administration pages.
+- database backend:
+    * fixed numereous SQL queries to make Drupal work on PostgreSQL and MSSQL.
 - search module:
-    * changed the search module to use implicit AND'ing instead of
-      implicit OR'ing.
+    * changed the search module to use implicit AND'ing instead of implicit OR'ing.
 - node system improvements:
-    * replaced the "post content" permission by more fine-grained
-      permissions.
+    * replaced the "post content" permission by more fine-grained permissions.
     * improved content submission:
-        + improved teasers: teasers are now optional, teaser length can
-          be configured, teaser and body are edited in a single
-          textarea, users will no longer be bother with teasers when
-          the post is too short for a teaser.
-        + added the ability to preview both the short and the full
-          version of your posts.
-    * extended the node api, allows for more advanced uses and cleans
-      up the interaction between modules.
-    * added default node settings to control the behaviour for
-      promotion, moderation and other options.
+        + improved teasers: teasers are now optional, teaser length can be configured, teaser and body are edited in a single textarea, users will no longer be bother with teasers when the post is too short for a teaser.
+        + added the ability to preview both the short and the full version of your posts.
+    * extended the node api, allows for more advanced uses and cleans up the interaction between modules.
+    * added default node settings to control the behaviour for promotion, moderation and other options.
 - themes:
     * replaced theme "Goofy" by "Xtemplate", a template driven theme.
 
diff --git a/MAINTAINERS b/MAINTAINERS
index 116fd3ea039d220fda390437bccc47b0bb6c766e..ebaf53b1ba0b7bfa6890fe117eb2da99296ee353 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -40,6 +40,10 @@ POLL MODULE
 M: Steven Wittens <unconed@drop.org>
 S: maintained
 
+MSSQL PORT
+M: Moshe Weitzman <weitzman@tejasa.com>
+S: maintained
+
 POSTGRES PORT
 M: James Arthur <j_a_arthur@yahoo.com>
 S: maintained
diff --git a/database/database.mysql b/database/database.mysql
index b99b679510f268a408b33635c4245129318dfce5..859a2b928bd79ef09551087ac92d5cb9f9693cc6 100644
--- a/database/database.mysql
+++ b/database/database.mysql
@@ -104,7 +104,6 @@ CREATE TABLE cache (
   cid varchar(255) NOT NULL default '',
   data mediumtext,
   expire int(11) NOT NULL default '0',
-  created int(11) NOT NULL default '0',
   PRIMARY KEY  (cid)
 ) TYPE=MyISAM;
 
@@ -167,11 +166,9 @@ CREATE TABLE feed (
 
 CREATE TABLE forum (
   nid int(10) unsigned NOT NULL default '0',
-  tid int(10) unsigned NOT NULL default '0',
   icon varchar(255) NOT NULL default '',
   shadow int(10) unsigned NOT NULL default '0',
-  PRIMARY KEY  (nid),
-  KEY tid (tid)
+  PRIMARY KEY  (nid)
 ) TYPE=MyISAM;
 
 --
@@ -239,8 +236,8 @@ CREATE TABLE moderation_roles (
   rid int(10) unsigned NOT NULL default '0',
   mid int(10) unsigned NOT NULL default '0',
   value tinyint(4) NOT NULL default '0',
-  KEY rid (rid),
-  KEY mid (mid)
+  KEY idx_rid (rid),
+  KEY idx_mid (mid)
 ) TYPE=MyISAM;
 
 --
@@ -541,7 +538,6 @@ CREATE TABLE watchdog (
   uid int(10) NOT NULL default '0',
   type varchar(16) NOT NULL default '',
   message text NOT NULL,
-  link varchar(255) NOT NULL default '',
   location varchar(128) NOT NULL default '',
   hostname varchar(128) NOT NULL default '',
   timestamp int(11) NOT NULL default '0',
diff --git a/includes/conf.php b/includes/conf.php
index ae59d1047a410066482f953f7c6bf053df4be677..920a94397bd18117d28724830a67982567ae31e3 100644
--- a/includes/conf.php
+++ b/includes/conf.php
@@ -12,8 +12,9 @@
 #   and so on is likely to confuse the parser; use alpha-numerical
 #   characters instead.
 
-# $db_url = "pgsql://user:password@hostname/database";
 # $db_url = "mysql://user:password@hostname/database";
+# $db_url = "pgsql://user:password@hostname/database";
+# $db_url = "mssql://user:password@hostname/database";
 
 $db_url = "mysql://drupal:drupal@localhost/drupal";
 
diff --git a/includes/database.pear.inc b/includes/database.pear.inc
index 4a5faef9b8636b5b88a28034710ce0c2de6b1339..0b56efcdf8c0af3fd0175b5c2c1aeacd4a82791d 100644
--- a/includes/database.pear.inc
+++ b/includes/database.pear.inc
@@ -23,6 +23,7 @@ function db_connect($url) {
  * @return sql result resource
  */
 function db_query($query) {
+
   $args = func_get_args();
   if (count($args) > 1) {
     $args = array_map("check_query", $args);
@@ -52,11 +53,19 @@ function _db_query($query, $debug = 0) {
   global $db_handle, $queries;
 
   if (variable_get("dev_query", 0)) {
-    $queries[] = $query;
+    list($usec, $sec) = explode(" ", microtime());
+    $timer = (float)$usec + (float)$sec;
   }
 
   $result = $db_handle->query($query);
 
+  if (variable_get("dev_query", 0)) {
+    list($usec, $sec) = explode(" ", microtime());
+    $stop = (float)$usec + (float)$sec;
+    $diff = $stop - $timer;
+    $queries[] = array($query, $diff);
+  }
+
   if ($debug) {
     print "<p>query: $query</p>";
   }
@@ -103,7 +112,13 @@ function db_error() {
 function db_next_id($name) {
   global $db_handle;
 
-  return $db_handle->nextID($name);
+  $result = $db_handle->nextID($name);
+  if (DB::isError($result)) {
+    watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($query));
+  }
+  else {
+    return $result;
+  }
 }
 
 function db_affected_rows() {
@@ -113,21 +128,46 @@ function db_affected_rows() {
 }
 
 /**
- * Generates a limited query
- *
- * @param string  $query query
- * @param integer $from  the row to start to fetching
- * @param integer $count the numbers of rows to fetch
+ * Runs a LIMIT query in the database.
  *
+ * @param $query sql query followed by 'from' and 'count' parameters, followed by a variable number of arguments which are substituted into query by sprintf. 'from' is the row to start to fetching. 'count' the numbers of rows to fetch.
  * @return mixed a DB_Result object or a DB_Error
  *
  * @access public
  */
+function db_query_range($query) {
+  global $db_handle, $queries;
 
-function db_query_range($query, $from, $count) {
-  global $db_handle;
+  if (variable_get("dev_query", 0)) {
+    list($usec, $sec) = explode(" ", microtime());
+    $timer = (float)$usec + (float)$sec;
+  }
 
-  return $db_handle->limitQuery($query, $from, $count);
+  $args = func_get_args();
+  $count = array_pop($args);
+  $from = array_pop($args);
+  if (count(func_get_args()) > 3) {
+    $args = array_map("check_query", $args);
+    $args[0] = $query;
+    $result = $db_handle->limitQuery(call_user_func_array("sprintf", $args), $from, $count);
+  }
+  else {
+    $result = $db_handle->limitQuery(func_get_arg(0), $from, $count);
+  }
+
+  if (variable_get("dev_query", 0)) {
+    list($usec, $sec) = explode(" ", microtime());
+    $stop = (float)$usec + (float)$sec;
+    $diff = $stop - $timer;
+    $queries[] = array($query. " [LIMIT $from, $count]", $diff);
+  }
+
+  if (DB::isError($result)) {
+    watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($query));
+  }
+  else {
+    return $result;
+  }
 }
 
-?>
+?>
\ No newline at end of file
diff --git a/includes/pager.inc b/includes/pager.inc
index 6f5a387734f3bb99257989a67b8f229e89f19c06..c31693ec4b84b47d579c0b6248e1c329df384204 100644
--- a/includes/pager.inc
+++ b/includes/pager.inc
@@ -287,6 +287,7 @@ function pager_query($query, $limit = 10, $element = 0, $count_query = "") {
   // count the total number of records in this query:
   if ($count_query == "") {
     $pager_total[$element] = db_result(db_query(preg_replace(array("/SELECT.*FROM/i", "/ORDER BY .*/"), array("SELECT COUNT(*) FROM", ""), $query)));
+
   }
   else {
     $pager_total[$element] = db_result(db_query($count_query));
@@ -296,6 +297,7 @@ function pager_query($query, $limit = 10, $element = 0, $count_query = "") {
   $pager_from_array = explode(",", $from);
 
   return db_query_range($query, (int)$pager_from_array[$element], (int)$limit);
+
 }
 
 function pager_link($from_new, $attributes = array()) {
@@ -329,4 +331,4 @@ function pager_load_array($value, $element, $old_array) {
   return $new_array;
 }
 
-?>
+?>
\ No newline at end of file
diff --git a/modules/aggregator.module b/modules/aggregator.module
index dbe09708161b74e5ebae78c3e42e4deb00314a13..91b830cdab3366171a545b7d8bae2de19dbd47fc 100644
--- a/modules/aggregator.module
+++ b/modules/aggregator.module
@@ -81,7 +81,7 @@ function import_bundle_block($attributes) {
     $keys = explode(",", $attributes);
     foreach ($keys as $key) $where[] = "attributes LIKE '%". trim($key) ."%'";
 
-    $result = db_query("SELECT * FROM item WHERE ". implode(" OR ", $where) ." ORDER BY iid DESC LIMIT ". variable_get("import_block_limit", 15));
+    $result = db_query_range("SELECT * FROM item WHERE ". implode(" OR ", $where) ." ORDER BY iid DESC", 0, variable_get("import_block_limit", 15));
   }
 
   while ($item = db_fetch_object($result)) {
@@ -92,7 +92,7 @@ function import_bundle_block($attributes) {
 }
 
 function import_feed_block($feed) {
-  $result = db_query("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC LIMIT ". variable_get("import_block_limit", 15), $feed->fid);
+  $result = db_query_range("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC ", 0, variable_get("import_block_limit", 15), $feed->fid);
 
   while ($item = db_fetch_object($result)) {
     $output .= import_format_item($item);
@@ -429,7 +429,7 @@ function import_view() {
 
 function import_tag() {
 
-  $result = db_query("SELECT i.*, f.title AS feed FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC LIMIT 50");
+  $result = db_query_range("SELECT i.*, f.title AS feed FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC", 0, 50);
 
   $header = array(t("date"), t("feed"), t("news item"));
   while ($item = db_fetch_object($result)) {
@@ -519,7 +519,7 @@ function import_page_info() {
 function import_page_last() {
 
 
-  $result = db_query("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC LIMIT ". variable_get("import_page_limit", 75));
+  $result = db_query_range("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC", 0, variable_get("import_page_limit", 75));
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
@@ -555,7 +555,7 @@ function import_page_feed($fid) {
   $header .= "<p><b>". t("Description") .":</b><div style=\"margin-left: 20px;\">$feed->description</div></p>";
   $header .= "<p><b>". t("Last update") .":</b><div style=\"margin-left: 20px;\">". format_interval(time() - $feed->timestamp) ." ". t("ago") ." <a href=\"$feed->url\"><img src=\"". theme("image", "xml.gif") ."\" width=\"36\" height=\"14\" align=\"right\" border=\"0\" alt=\"\" /></a><br /><br /></div></p>\n";
 
-  $result = db_query("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC LIMIT ". variable_get("import_page_limit", 75), $fid);
+  $result = db_query_range("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC", 0, variable_get("import_page_limit", 75), $fid);
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
@@ -592,7 +592,7 @@ function import_page_bundle($bid) {
 
   $keys = explode(",", $bundle->attributes);
   foreach ($keys as $key) $where[] = "i.attributes LIKE '%". trim($key) ."%'";
-  $result = db_query("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i, feed f WHERE (". implode(" OR ", $where) .") AND i.fid = f.fid ORDER BY iid DESC LIMIT ". variable_get("import_page_limit", 75));
+  $result = db_query_range("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i, feed f WHERE (". implode(" OR ", $where) .") AND i.fid = f.fid ORDER BY iid DESC", 0, variable_get("import_page_limit", 75));
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
diff --git a/modules/aggregator/aggregator.module b/modules/aggregator/aggregator.module
index dbe09708161b74e5ebae78c3e42e4deb00314a13..91b830cdab3366171a545b7d8bae2de19dbd47fc 100644
--- a/modules/aggregator/aggregator.module
+++ b/modules/aggregator/aggregator.module
@@ -81,7 +81,7 @@ function import_bundle_block($attributes) {
     $keys = explode(",", $attributes);
     foreach ($keys as $key) $where[] = "attributes LIKE '%". trim($key) ."%'";
 
-    $result = db_query("SELECT * FROM item WHERE ". implode(" OR ", $where) ." ORDER BY iid DESC LIMIT ". variable_get("import_block_limit", 15));
+    $result = db_query_range("SELECT * FROM item WHERE ". implode(" OR ", $where) ." ORDER BY iid DESC", 0, variable_get("import_block_limit", 15));
   }
 
   while ($item = db_fetch_object($result)) {
@@ -92,7 +92,7 @@ function import_bundle_block($attributes) {
 }
 
 function import_feed_block($feed) {
-  $result = db_query("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC LIMIT ". variable_get("import_block_limit", 15), $feed->fid);
+  $result = db_query_range("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC ", 0, variable_get("import_block_limit", 15), $feed->fid);
 
   while ($item = db_fetch_object($result)) {
     $output .= import_format_item($item);
@@ -429,7 +429,7 @@ function import_view() {
 
 function import_tag() {
 
-  $result = db_query("SELECT i.*, f.title AS feed FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC LIMIT 50");
+  $result = db_query_range("SELECT i.*, f.title AS feed FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC", 0, 50);
 
   $header = array(t("date"), t("feed"), t("news item"));
   while ($item = db_fetch_object($result)) {
@@ -519,7 +519,7 @@ function import_page_info() {
 function import_page_last() {
 
 
-  $result = db_query("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC LIMIT ". variable_get("import_page_limit", 75));
+  $result = db_query_range("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC", 0, variable_get("import_page_limit", 75));
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
@@ -555,7 +555,7 @@ function import_page_feed($fid) {
   $header .= "<p><b>". t("Description") .":</b><div style=\"margin-left: 20px;\">$feed->description</div></p>";
   $header .= "<p><b>". t("Last update") .":</b><div style=\"margin-left: 20px;\">". format_interval(time() - $feed->timestamp) ." ". t("ago") ." <a href=\"$feed->url\"><img src=\"". theme("image", "xml.gif") ."\" width=\"36\" height=\"14\" align=\"right\" border=\"0\" alt=\"\" /></a><br /><br /></div></p>\n";
 
-  $result = db_query("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC LIMIT ". variable_get("import_page_limit", 75), $fid);
+  $result = db_query_range("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC", 0, variable_get("import_page_limit", 75), $fid);
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
@@ -592,7 +592,7 @@ function import_page_bundle($bid) {
 
   $keys = explode(",", $bundle->attributes);
   foreach ($keys as $key) $where[] = "i.attributes LIKE '%". trim($key) ."%'";
-  $result = db_query("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i, feed f WHERE (". implode(" OR ", $where) .") AND i.fid = f.fid ORDER BY iid DESC LIMIT ". variable_get("import_page_limit", 75));
+  $result = db_query_range("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i, feed f WHERE (". implode(" OR ", $where) .") AND i.fid = f.fid ORDER BY iid DESC", 0, variable_get("import_page_limit", 75));
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
diff --git a/modules/archive.module b/modules/archive.module
index 6395c693da7f2d47b11d4941f2519b796cee31fc..be3555d1468d7e3230be9619089722c4a512a9ba 100644
--- a/modules/archive.module
+++ b/modules/archive.module
@@ -174,7 +174,7 @@ function archive_page() {
     */
 
     if ($year && $month && $day) {
-      $result = db_query("SELECT nid FROM node WHERE status = '1' AND created > %d ORDER BY created LIMIT 20", $date);
+      $result = db_query_range("SELECT nid FROM node WHERE status = '1' AND created > %d ORDER BY created", $date, 0, 20);
 
       while ($nid = db_fetch_object($result)) {
         node_view(node_load(array("nid" => $nid->nid)), 1);
diff --git a/modules/archive/archive.module b/modules/archive/archive.module
index 6395c693da7f2d47b11d4941f2519b796cee31fc..be3555d1468d7e3230be9619089722c4a512a9ba 100644
--- a/modules/archive/archive.module
+++ b/modules/archive/archive.module
@@ -174,7 +174,7 @@ function archive_page() {
     */
 
     if ($year && $month && $day) {
-      $result = db_query("SELECT nid FROM node WHERE status = '1' AND created > %d ORDER BY created LIMIT 20", $date);
+      $result = db_query_range("SELECT nid FROM node WHERE status = '1' AND created > %d ORDER BY created", $date, 0, 20);
 
       while ($nid = db_fetch_object($result)) {
         node_view(node_load(array("nid" => $nid->nid)), 1);
diff --git a/modules/blog.module b/modules/blog.module
index 8646ea3f3c156625f825a6b160e791845bb22473..f8cd33d1f8da46bb6027db46a01054450766f6b5 100644
--- a/modules/blog.module
+++ b/modules/blog.module
@@ -86,7 +86,7 @@ function blog_feed_user($uid = 0) {
     $account = $user;
   }
 
-  $result = db_query("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND u.uid = '%d' AND n.status = 1 ORDER BY n.nid DESC LIMIT 15", $uid);
+  $result = db_query_range("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND u.uid = '%d' AND n.status = 1 ORDER BY n.nid DESC", $uid, 0, 15);
   $channel["title"] = $account->name. "'s blog";
   $channel["link"] = url("blog/view/$uid");
   $channel["description"] = $term->description;
@@ -94,7 +94,7 @@ function blog_feed_user($uid = 0) {
 }
 
 function blog_feed_last() {
-  $result = db_query("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC LIMIT 15");
+  $result = db_query_range("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC", 0, 15);
   $channel["title"] = variable_get("site_name", "drupal") ." blogs";
   $channel["link"] = url("blog/view");
   $channel["description"] = $term->description;
@@ -242,7 +242,7 @@ function blog_block($op = "list", $delta = 0) {
   }
   else {
     if (user_access("access content")) {
-      $block["content"] = node_title_list(db_query("SELECT u.uid, u.name, n.created, n.title, n.nid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC LIMIT 10"));
+      $block["content"] = node_title_list(db_query_range("SELECT u.uid, u.name, n.created, n.title, n.nid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC", 0, 10));
       $block["content"] .= "<div align=\"right\" id=\"blog_more\">". l(t("more"), "blog", array("title" => t("Read the latest blog entries."))) ."</div>";
       $block["subject"] = t("User blogs");
     }
diff --git a/modules/blog/blog.module b/modules/blog/blog.module
index 8646ea3f3c156625f825a6b160e791845bb22473..f8cd33d1f8da46bb6027db46a01054450766f6b5 100644
--- a/modules/blog/blog.module
+++ b/modules/blog/blog.module
@@ -86,7 +86,7 @@ function blog_feed_user($uid = 0) {
     $account = $user;
   }
 
-  $result = db_query("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND u.uid = '%d' AND n.status = 1 ORDER BY n.nid DESC LIMIT 15", $uid);
+  $result = db_query_range("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND u.uid = '%d' AND n.status = 1 ORDER BY n.nid DESC", $uid, 0, 15);
   $channel["title"] = $account->name. "'s blog";
   $channel["link"] = url("blog/view/$uid");
   $channel["description"] = $term->description;
@@ -94,7 +94,7 @@ function blog_feed_user($uid = 0) {
 }
 
 function blog_feed_last() {
-  $result = db_query("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC LIMIT 15");
+  $result = db_query_range("SELECT n.nid, n.title, n.teaser, n.created, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC", 0, 15);
   $channel["title"] = variable_get("site_name", "drupal") ." blogs";
   $channel["link"] = url("blog/view");
   $channel["description"] = $term->description;
@@ -242,7 +242,7 @@ function blog_block($op = "list", $delta = 0) {
   }
   else {
     if (user_access("access content")) {
-      $block["content"] = node_title_list(db_query("SELECT u.uid, u.name, n.created, n.title, n.nid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC LIMIT 10"));
+      $block["content"] = node_title_list(db_query_range("SELECT u.uid, u.name, n.created, n.title, n.nid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.nid DESC", 0, 10));
       $block["content"] .= "<div align=\"right\" id=\"blog_more\">". l(t("more"), "blog", array("title" => t("Read the latest blog entries."))) ."</div>";
       $block["subject"] = t("User blogs");
     }
diff --git a/modules/bloggerapi.module b/modules/bloggerapi.module
index 722447f2f1414185e78012f7378c479977bc0c1d..93ce0dcc99d0f64ac9a5435ffd9286c3a5c0eaec 100644
--- a/modules/bloggerapi.module
+++ b/modules/bloggerapi.module
@@ -269,7 +269,7 @@ function bloggerapi_node_recent($num) {
   global $user;
 
   if (($num == 0) or ($num > 100)) $num = 50;
-  $result = db_query("SELECT n.*, u.name FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.uid = '%d' ORDER BY n.nid DESC LIMIT %d", $user->uid, $num);
+  $result = db_query_range("SELECT n.*, u.name FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.uid = '%d' ORDER BY n.nid DESC", $user->uid, 0, $num);
   if ($result) {
     while ($blog = db_fetch_object($result)) {
       $body = "<title>$blog->title</title>\n". $blog->body;
diff --git a/modules/book.module b/modules/book.module
index 5c5eb1b797359d243c9cfc7b545126fcade761a5..2d13bd46c7fcd269d8504189314bff5309215d90 100644
--- a/modules/book.module
+++ b/modules/book.module
@@ -513,7 +513,7 @@ function book_tree($parent = 0, $depth = 3) {
 function book_render() {
 
 
-  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight, n.title");
+  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title");
 
   while ($page = db_fetch_object($result)) {
     // load the node:
@@ -555,7 +555,7 @@ function book_page() {
 }
 
 function book_print($id = "", $depth = 1) {
-  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = '%d' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight, n.title", $id);
+  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = '%d' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title", $id);
 
   while ($page = db_fetch_object($result)) {
     // load the node:
@@ -582,7 +582,7 @@ function book_print($id = "", $depth = 1) {
 }
 
 function book_print_recurse($parent = "", $depth = 1) {
-  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight, n.title");
+  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title");
 
   while ($page = db_fetch_object($result)) {
     // load the node:
@@ -756,4 +756,4 @@ function book_help() {
 </ul>
   <?php
 }
-?>
+?>
\ No newline at end of file
diff --git a/modules/book/book.module b/modules/book/book.module
index 5c5eb1b797359d243c9cfc7b545126fcade761a5..2d13bd46c7fcd269d8504189314bff5309215d90 100644
--- a/modules/book/book.module
+++ b/modules/book/book.module
@@ -513,7 +513,7 @@ function book_tree($parent = 0, $depth = 3) {
 function book_render() {
 
 
-  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight, n.title");
+  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title");
 
   while ($page = db_fetch_object($result)) {
     // load the node:
@@ -555,7 +555,7 @@ function book_page() {
 }
 
 function book_print($id = "", $depth = 1) {
-  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = '%d' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight, n.title", $id);
+  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = '%d' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title", $id);
 
   while ($page = db_fetch_object($result)) {
     // load the node:
@@ -582,7 +582,7 @@ function book_print($id = "", $depth = 1) {
 }
 
 function book_print_recurse($parent = "", $depth = 1) {
-  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight, n.title");
+  $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title");
 
   while ($page = db_fetch_object($result)) {
     // load the node:
@@ -756,4 +756,4 @@ function book_help() {
 </ul>
   <?php
 }
-?>
+?>
\ No newline at end of file
diff --git a/modules/cloud.module b/modules/cloud.module
index fbfdc2501227907c62be29ff9b3a99c674e9c804..a796593c17e2ecea975e4806bd05d8088c7ac58a 100644
--- a/modules/cloud.module
+++ b/modules/cloud.module
@@ -134,7 +134,7 @@ function cloud_display() {
 }
 
 function cloud_list($limit = 10) {
-  $result = db_query("SELECT * FROM site WHERE timestamp > ". (time() - 604800) ." ORDER BY timestamp DESC LIMIT $limit");
+  $result = db_query_range("SELECT * FROM site WHERE timestamp > ". (time() - 604800) ." ORDER BY timestamp DESC", 0, $limit);
 
   $hour = -1;
   $list = -1;
diff --git a/modules/comment.module b/modules/comment.module
index b86560637067ff20b2a8966c9e0e52c9c3c9c0f3..49d6d1da989a46f7f76508aab8a3542946607eb0 100644
--- a/modules/comment.module
+++ b/modules/comment.module
@@ -797,7 +797,7 @@ function comment_save($id, $edit) {
 
 function comment_admin_overview($status = 0) {
 
-  $result = pager_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON u.uid = c.uid WHERE c.status = '". check_query($status) ."' ORDER BY c.timestamp DESC", 50);
+  $result = pager_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON u.uid = c.uid WHERE c.status = '". check_query($status). "' ORDER BY c.timestamp DESC",  50);
 
   $header = array(t("subject"), t("author"), t("status"), array("data" => t("operations"), "colspan" => 2));
   while ($comment = db_fetch_object($result)) {
@@ -1465,4 +1465,4 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
   }
 }
 
-?>
+?>
\ No newline at end of file
diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index b86560637067ff20b2a8966c9e0e52c9c3c9c0f3..49d6d1da989a46f7f76508aab8a3542946607eb0 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -797,7 +797,7 @@ function comment_save($id, $edit) {
 
 function comment_admin_overview($status = 0) {
 
-  $result = pager_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON u.uid = c.uid WHERE c.status = '". check_query($status) ."' ORDER BY c.timestamp DESC", 50);
+  $result = pager_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON u.uid = c.uid WHERE c.status = '". check_query($status). "' ORDER BY c.timestamp DESC",  50);
 
   $header = array(t("subject"), t("author"), t("status"), array("data" => t("operations"), "colspan" => 2));
   while ($comment = db_fetch_object($result)) {
@@ -1465,4 +1465,4 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
   }
 }
 
-?>
+?>
\ No newline at end of file
diff --git a/modules/forum.module b/modules/forum.module
index 3b87fc97cb5f52a41af20465d46fa7d3e04a57bf..cf4d9fd1b117d1eae40b221266e59e5d1031432c 100644
--- a/modules/forum.module
+++ b/modules/forum.module
@@ -85,11 +85,11 @@ function forum_block($op = "list", $delta = 0) {
 
       if (empty($cache)) {
         unset($items);
-        $content = node_title_list(db_query("SELECT n.nid, n.title, u.uid, u.name, GREATEST(n.created, MAX(c.timestamp)) AS sort FROM node n, forum f LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' AND n.nid = f.nid AND n.status = 1 GROUP BY n.nid ORDER BY sort DESC LIMIT ". variable_get("forum_block_num", "5")), t("Active forum topics:"));
+        $content = node_title_list(db_query_range("SELECT n.nid, n.title, u.uid, u.name, GREATEST(n.created, MAX(c.timestamp)) AS sort FROM node n, forum f LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' AND n.nid = f.nid AND n.status = 1 GROUP BY n.nid ORDER BY sort DESC", 0, variable_get("forum_block_num", "5")), t("Active forum topics:"));
         $content .= "<br />";
 
         unset ($items);
-        $content .= node_title_list(db_query("SELECT n.nid, n.title, u.uid, u.name FROM node n LEFT JOIN forum f ON n.nid = f.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' ORDER BY n.nid DESC LIMIT ". variable_get("forum_block_num", "5")), t("New forum topics:"));
+        $content .= node_title_list(db_query_range("SELECT n.nid, n.title, u.uid, u.name FROM node n LEFT JOIN forum f ON n.nid = f.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' ORDER BY n.nid DESC", 0, variable_get("forum_block_num", "5")), t("New forum topics:"));
 
         if ($content) {
           $content .= "<div id=\"forum_more\" align=\"right\">". l(t("more"), "forum") ."</div>";
@@ -272,12 +272,12 @@ function _forum_num_comments($nid) {
 }
 
 function _forum_last_comment($nid) {
-  $value = db_fetch_object(db_query("SELECT timestamp FROM comments WHERE nid = '%d' AND status = 0 ORDER BY timestamp DESC LIMIT 1", $nid));
+  $value = db_fetch_object(db_query_range("SELECT timestamp FROM comments WHERE nid = '%d' AND status = 0 ORDER BY timestamp DESC", $nid, 0, 1));
   return ($value) ? format_date($value->timestamp, "small") : "&nbsp;";
 }
 
 function _forum_last_reply($nid) {
-  $value = db_fetch_object(db_query("SELECT c.timestamp, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '%d' AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 1", $nid));
+  $value = db_fetch_object(db_query_range("SELECT c.timestamp, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '%d' AND c.status = 0 ORDER BY c.timestamp DESC", $nid, 0, 1));
   return $value;
 }
 
@@ -366,9 +366,9 @@ function _forum_topics_read($uid) {
 }
 
 function _forum_last_post($term) {
-  $topic = db_fetch_object(db_query("SELECT n.nid, n.created AS timestamp, u.name AS name, u.uid AS uid FROM node n, forum f LEFT JOIN users u ON n.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 ORDER BY timestamp DESC LIMIT 1", $term));
+  $topic = db_fetch_object(db_query_range("SELECT n.nid, n.created AS timestamp, u.name AS name, u.uid AS uid FROM  forum f, node n LEFT JOIN users u ON n.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 ORDER BY timestamp DESC", $term, 0, 1));
 
-  $reply = db_fetch_object(db_query("SELECT n.nid, c.timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN comments c ON n.nid = c.nid LEFT JOIN users u ON c.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 1", $term));
+  $reply = db_fetch_object(db_query_range("SELECT n.nid, c.timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN comments c ON n.nid = c.nid LEFT JOIN users u ON c.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC", $term, 0, 1));
 
   $value = ($topic->timestamp > $reply->timestamp) ? $topic : $reply;
 
@@ -386,7 +386,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) {
   // show topics with the correct tid, or in the forum but with shadow = 1
   $sql = "SELECT n.nid, title, u.name AS name, u.uid AS uid, n.created AS timestamp, GREATEST(n.created, MAX(c.timestamp)) AS date_sort, COUNT(c.nid) AS num_comments, icon, n.comment AS comment_mode, f.tid FROM node n, term_node r LEFT JOIN users u ON n.uid = u.uid LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum' GROUP BY n.nid ORDER BY $sql_sortby";
 
-  $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM node n, term_node r LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum'";
+  $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM term_node r, node n LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum'";
 
   $result = pager_query($sql, $forum_per_page, 0, $sql_count);
   $topic_num = db_num_rows($result);
@@ -437,7 +437,7 @@ function _forum_new($tid) {
     $read[] = $r->nid;
   }
 
-  $nid = db_result(db_query("SELECT n.nid FROM node n, forum f WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = '%d' ".($read ? "AND NOT (n.nid IN (".implode(",", $read).")) " : "") ."ORDER BY created LIMIT 1", $tid));
+  $nid = db_result(db_query_range("SELECT n.nid FROM node n, forum f WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = '%d' ".($read ? "AND NOT (n.nid IN (".implode(",", $read).")) " : "") ."ORDER BY created", $tid, 0, 1));
 
   return $nid ? $nid : 0;
 }
diff --git a/modules/forum/forum.module b/modules/forum/forum.module
index 3b87fc97cb5f52a41af20465d46fa7d3e04a57bf..cf4d9fd1b117d1eae40b221266e59e5d1031432c 100644
--- a/modules/forum/forum.module
+++ b/modules/forum/forum.module
@@ -85,11 +85,11 @@ function forum_block($op = "list", $delta = 0) {
 
       if (empty($cache)) {
         unset($items);
-        $content = node_title_list(db_query("SELECT n.nid, n.title, u.uid, u.name, GREATEST(n.created, MAX(c.timestamp)) AS sort FROM node n, forum f LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' AND n.nid = f.nid AND n.status = 1 GROUP BY n.nid ORDER BY sort DESC LIMIT ". variable_get("forum_block_num", "5")), t("Active forum topics:"));
+        $content = node_title_list(db_query_range("SELECT n.nid, n.title, u.uid, u.name, GREATEST(n.created, MAX(c.timestamp)) AS sort FROM node n, forum f LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' AND n.nid = f.nid AND n.status = 1 GROUP BY n.nid ORDER BY sort DESC", 0, variable_get("forum_block_num", "5")), t("Active forum topics:"));
         $content .= "<br />";
 
         unset ($items);
-        $content .= node_title_list(db_query("SELECT n.nid, n.title, u.uid, u.name FROM node n LEFT JOIN forum f ON n.nid = f.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' ORDER BY n.nid DESC LIMIT ". variable_get("forum_block_num", "5")), t("New forum topics:"));
+        $content .= node_title_list(db_query_range("SELECT n.nid, n.title, u.uid, u.name FROM node n LEFT JOIN forum f ON n.nid = f.nid LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'forum' ORDER BY n.nid DESC", 0, variable_get("forum_block_num", "5")), t("New forum topics:"));
 
         if ($content) {
           $content .= "<div id=\"forum_more\" align=\"right\">". l(t("more"), "forum") ."</div>";
@@ -272,12 +272,12 @@ function _forum_num_comments($nid) {
 }
 
 function _forum_last_comment($nid) {
-  $value = db_fetch_object(db_query("SELECT timestamp FROM comments WHERE nid = '%d' AND status = 0 ORDER BY timestamp DESC LIMIT 1", $nid));
+  $value = db_fetch_object(db_query_range("SELECT timestamp FROM comments WHERE nid = '%d' AND status = 0 ORDER BY timestamp DESC", $nid, 0, 1));
   return ($value) ? format_date($value->timestamp, "small") : "&nbsp;";
 }
 
 function _forum_last_reply($nid) {
-  $value = db_fetch_object(db_query("SELECT c.timestamp, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '%d' AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 1", $nid));
+  $value = db_fetch_object(db_query_range("SELECT c.timestamp, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '%d' AND c.status = 0 ORDER BY c.timestamp DESC", $nid, 0, 1));
   return $value;
 }
 
@@ -366,9 +366,9 @@ function _forum_topics_read($uid) {
 }
 
 function _forum_last_post($term) {
-  $topic = db_fetch_object(db_query("SELECT n.nid, n.created AS timestamp, u.name AS name, u.uid AS uid FROM node n, forum f LEFT JOIN users u ON n.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 ORDER BY timestamp DESC LIMIT 1", $term));
+  $topic = db_fetch_object(db_query_range("SELECT n.nid, n.created AS timestamp, u.name AS name, u.uid AS uid FROM  forum f, node n LEFT JOIN users u ON n.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 ORDER BY timestamp DESC", $term, 0, 1));
 
-  $reply = db_fetch_object(db_query("SELECT n.nid, c.timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN comments c ON n.nid = c.nid LEFT JOIN users u ON c.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 1", $term));
+  $reply = db_fetch_object(db_query_range("SELECT n.nid, c.timestamp, u.name AS name, u.uid AS uid FROM forum f, node n LEFT JOIN comments c ON n.nid = c.nid LEFT JOIN users u ON c.uid = u.uid WHERE f.tid = '%d' AND n.nid = f.nid AND n.type = 'forum' AND n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC", $term, 0, 1));
 
   $value = ($topic->timestamp > $reply->timestamp) ? $topic : $reply;
 
@@ -386,7 +386,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) {
   // show topics with the correct tid, or in the forum but with shadow = 1
   $sql = "SELECT n.nid, title, u.name AS name, u.uid AS uid, n.created AS timestamp, GREATEST(n.created, MAX(c.timestamp)) AS date_sort, COUNT(c.nid) AS num_comments, icon, n.comment AS comment_mode, f.tid FROM node n, term_node r LEFT JOIN users u ON n.uid = u.uid LEFT JOIN comments c ON c.nid = n.nid LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum' GROUP BY n.nid ORDER BY $sql_sortby";
 
-  $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM node n, term_node r LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum'";
+  $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM term_node r, node n LEFT JOIN forum f ON n.nid = f.nid WHERE n.nid = r.nid AND ( (r.tid = '".check_query($tid)."' AND f.shadow = 1) OR f.tid = '".check_query($tid)."' ) AND n.status = 1 AND n.type = 'forum'";
 
   $result = pager_query($sql, $forum_per_page, 0, $sql_count);
   $topic_num = db_num_rows($result);
@@ -437,7 +437,7 @@ function _forum_new($tid) {
     $read[] = $r->nid;
   }
 
-  $nid = db_result(db_query("SELECT n.nid FROM node n, forum f WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = '%d' ".($read ? "AND NOT (n.nid IN (".implode(",", $read).")) " : "") ."ORDER BY created LIMIT 1", $tid));
+  $nid = db_result(db_query_range("SELECT n.nid FROM node n, forum f WHERE n.type = 'forum' AND f.nid = n.nid AND n.status = 1 AND f.tid = '%d' ".($read ? "AND NOT (n.nid IN (".implode(",", $read).")) " : "") ."ORDER BY created", $tid, 0, 1));
 
   return $nid ? $nid : 0;
 }
diff --git a/modules/import.module b/modules/import.module
index dbe09708161b74e5ebae78c3e42e4deb00314a13..91b830cdab3366171a545b7d8bae2de19dbd47fc 100644
--- a/modules/import.module
+++ b/modules/import.module
@@ -81,7 +81,7 @@ function import_bundle_block($attributes) {
     $keys = explode(",", $attributes);
     foreach ($keys as $key) $where[] = "attributes LIKE '%". trim($key) ."%'";
 
-    $result = db_query("SELECT * FROM item WHERE ". implode(" OR ", $where) ." ORDER BY iid DESC LIMIT ". variable_get("import_block_limit", 15));
+    $result = db_query_range("SELECT * FROM item WHERE ". implode(" OR ", $where) ." ORDER BY iid DESC", 0, variable_get("import_block_limit", 15));
   }
 
   while ($item = db_fetch_object($result)) {
@@ -92,7 +92,7 @@ function import_bundle_block($attributes) {
 }
 
 function import_feed_block($feed) {
-  $result = db_query("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC LIMIT ". variable_get("import_block_limit", 15), $feed->fid);
+  $result = db_query_range("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC ", 0, variable_get("import_block_limit", 15), $feed->fid);
 
   while ($item = db_fetch_object($result)) {
     $output .= import_format_item($item);
@@ -429,7 +429,7 @@ function import_view() {
 
 function import_tag() {
 
-  $result = db_query("SELECT i.*, f.title AS feed FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC LIMIT 50");
+  $result = db_query_range("SELECT i.*, f.title AS feed FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC", 0, 50);
 
   $header = array(t("date"), t("feed"), t("news item"));
   while ($item = db_fetch_object($result)) {
@@ -519,7 +519,7 @@ function import_page_info() {
 function import_page_last() {
 
 
-  $result = db_query("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC LIMIT ". variable_get("import_page_limit", 75));
+  $result = db_query_range("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i LEFT JOIN feed f ON i.fid = f.fid ORDER BY i.iid DESC", 0, variable_get("import_page_limit", 75));
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
@@ -555,7 +555,7 @@ function import_page_feed($fid) {
   $header .= "<p><b>". t("Description") .":</b><div style=\"margin-left: 20px;\">$feed->description</div></p>";
   $header .= "<p><b>". t("Last update") .":</b><div style=\"margin-left: 20px;\">". format_interval(time() - $feed->timestamp) ." ". t("ago") ." <a href=\"$feed->url\"><img src=\"". theme("image", "xml.gif") ."\" width=\"36\" height=\"14\" align=\"right\" border=\"0\" alt=\"\" /></a><br /><br /></div></p>\n";
 
-  $result = db_query("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC LIMIT ". variable_get("import_page_limit", 75), $fid);
+  $result = db_query_range("SELECT * FROM item WHERE fid = '%s' ORDER BY iid DESC", 0, variable_get("import_page_limit", 75), $fid);
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
@@ -592,7 +592,7 @@ function import_page_bundle($bid) {
 
   $keys = explode(",", $bundle->attributes);
   foreach ($keys as $key) $where[] = "i.attributes LIKE '%". trim($key) ."%'";
-  $result = db_query("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i, feed f WHERE (". implode(" OR ", $where) .") AND i.fid = f.fid ORDER BY iid DESC LIMIT ". variable_get("import_page_limit", 75));
+  $result = db_query_range("SELECT i.*, f.title AS ftitle, f.link AS flink FROM item i, feed f WHERE (". implode(" OR ", $where) .") AND i.fid = f.fid ORDER BY iid DESC", 0, variable_get("import_page_limit", 75));
 
   $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
   while ($item = db_fetch_object($result)) {
diff --git a/modules/locale.module b/modules/locale.module
index d886b16fc249cf974fba1d01249e2a57d629713a..8486503410c307255e2555100a2d128b4c1e949a 100644
--- a/modules/locale.module
+++ b/modules/locale.module
@@ -142,7 +142,7 @@ function locale_seek() {
           foreach ($languages as $key=>$value) {
             $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
           }
-          $query[] = implode(" && ", $tmp);
+          $query[] = implode(" AND ", $tmp);
           break;
         case "any":
           foreach ($languages as $key=>$value) {
diff --git a/modules/locale/locale.module b/modules/locale/locale.module
index d886b16fc249cf974fba1d01249e2a57d629713a..8486503410c307255e2555100a2d128b4c1e949a 100644
--- a/modules/locale/locale.module
+++ b/modules/locale/locale.module
@@ -142,7 +142,7 @@ function locale_seek() {
           foreach ($languages as $key=>$value) {
             $tmp[] = $key . (check_query($edit["status"]) == 1 ? " !=" : " =") ." ''";
           }
-          $query[] = implode(" && ", $tmp);
+          $query[] = implode(" AND ", $tmp);
           break;
         case "any":
           foreach ($languages as $key=>$value) {
diff --git a/modules/node.module b/modules/node.module
index 04611cdd9a3755731b1e7db4d1201a034a5c9506..a4eae222cd7f74828aaec66d3e3e7a75800d9501 100644
--- a/modules/node.module
+++ b/modules/node.module
@@ -835,7 +835,7 @@ function node_feed($nodes = 0, $channel = array()) {
   */
 
   if (!$nodes) {
-    $nodes = db_query("SELECT nid FROM node WHERE promote = '1' AND status = '1' ORDER BY created DESC LIMIT 15");
+    $nodes = db_query_range("SELECT nid FROM node WHERE promote = '1' AND status = '1' ORDER BY created DESC", 0, 15);
   }
 
   while ($node = db_fetch_object($nodes)) {
diff --git a/modules/node/node.module b/modules/node/node.module
index 04611cdd9a3755731b1e7db4d1201a034a5c9506..a4eae222cd7f74828aaec66d3e3e7a75800d9501 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -835,7 +835,7 @@ function node_feed($nodes = 0, $channel = array()) {
   */
 
   if (!$nodes) {
-    $nodes = db_query("SELECT nid FROM node WHERE promote = '1' AND status = '1' ORDER BY created DESC LIMIT 15");
+    $nodes = db_query_range("SELECT nid FROM node WHERE promote = '1' AND status = '1' ORDER BY created DESC", 0, 15);
   }
 
   while ($node = db_fetch_object($nodes)) {
diff --git a/modules/statistics.module b/modules/statistics.module
index afa850d7941f8bc2d54143be0b9f85121031bb7a..7100ad2ec6344475ed915d7001231e40f359bbc1 100644
--- a/modules/statistics.module
+++ b/modules/statistics.module
@@ -323,7 +323,7 @@ function statistics_admin() {
 
 /* Displays the various admin tables */
 function statistics_admin_count_table($dbfield, $dbrows) {
-  $result = db_query("SELECT statistics.nid, statistics.daycount, statistics.totalcount, statistics.timestamp, node.title FROM statistics LEFT JOIN node USING (nid) WHERE statistics.%s <> '0' ORDER BY statistics.%s DESC LIMIT %s", $dbfield, $dbfield, $dbrows);
+  $result = db_query_range("SELECT statistics.nid, statistics.daycount, statistics.totalcount, statistics.timestamp, node.title FROM statistics LEFT JOIN node USING (nid) WHERE statistics.%s <> '0' ORDER BY statistics.%s DESC", $dbfield, $dbfield, 0, $dbrows);
 
   $header = array(t("title"), t("today"), t("all time"), t("last hit"), t("operations"));
 
@@ -346,24 +346,24 @@ function statistics_admin_accesslog_table($type, $id) {
     /* retrieve user access logs */
     if ($id) {
       /* retrieve recent access logs for user $id */
-      $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE uid = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
+      $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE uid = '%s' ORDER BY timestamp DESC", $id, 0, $limit1);
     }
     else {
       /* retrieve recent access logs for all users */
-      $result = db_query("SELECT nid, url, hostname, uid, MAX(timestamp) AS timestamp FROM accesslog WHERE uid <> '0' GROUP BY uid, nid, url, hostname ORDER BY timestamp DESC LIMIT %s", $limit1);
+      $result = db_query_range("SELECT nid, url, hostname, uid, MAX(timestamp) AS timestamp FROM accesslog WHERE uid <> '0' GROUP BY uid, nid, url, hostname ORDER BY timestamp DESC", 0, $limit1);
     }
   }
   else if ($type == 2) {
     /* retrieve recent access logs for node $id */
-    $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE nid = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
+    $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE nid = '%s' ORDER BY timestamp DESC", $id, 0, $limit1);
   }
   else if ($type == 3) {
     /* retrieve recent access logs for hostname $id */
-    $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE hostname = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
+    $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE hostname = '%s' ORDER BY timestamp DESC %s", $id, 0, $limit1);
   }
   else {
     /* retrieve all recent access logs */
-    $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog ORDER BY timestamp DESC LIMIT %s", $limit0);
+    $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog ORDER BY timestamp DESC", 0, $limit0);
   }
 
   $header = array(t("timestamp"), t("title"), t("user"), t("hostname"), t("referrer"));
@@ -399,31 +399,31 @@ function statistics_recent_refer($nid = 0) {
 
   if ($nid > 0) {
     if ($view == "all") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url <> '' ORDER BY timestamp DESC";
     }
     elseif ($view == "internal") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC";
       $describe = "internal ";
     }
     else {
       /* default to external referrers */
-      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC";
       $describe = "external ";
     }
 
-    $result = db_query($query);
+    $result = db_query_range($query, 0, 15);
     $output = "<h3>". t("Most recent %describe referrers for node", array("%describe" => $describe)) ."\"". l($node->title, "node/view/$nid", array("title" => t("View this posting."))) ."\"</h3>";
   }
   else {
     if ($view == "all") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE url <> '' ORDER BY timestamp DESC";
     }
     elseif ($view == "internal") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC";
       $describe = "internal ";
     }
     else {
-      $query = "SELECT url,timestamp FROM accesslog WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC";
       $describe = "external ";
     }
 
@@ -792,7 +792,7 @@ function statistics_display_online_block() {
 /* Display linked title based on field name */
 function statistics_title_list($dbfield, $dbrows) {
   /* valid dbfields: totalcount, daycount, timestamp */
-  return db_query("SELECT statistics.nid, node.title, u.uid, u.name FROM statistics LEFT JOIN node ON statistics.nid = node.nid LEFT JOIN users u ON node.uid = u.uid WHERE %s <> '0' AND node.status = 1 ORDER BY %s DESC LIMIT %s", "statistics.". $dbfield, "statistics.". $dbfield, $dbrows);
+  return db_query_range("SELECT statistics.nid, node.title, u.uid, u.name FROM statistics LEFT JOIN node ON statistics.nid = node.nid LEFT JOIN users u ON node.uid = u.uid WHERE %s <> '0' AND node.status = 1 ORDER BY %s DESC", "statistics.". $dbfield, "statistics.". $dbfield, 0, $dbrows);
 }
 
 
@@ -890,7 +890,7 @@ function statistics_summary($dbfield, $dbrows) {
 
 
   $output = "";
-  $result = db_query("SELECT statistics.nid,node.title FROM statistics LEFT JOIN node ON statistics.nid = node.nid ORDER BY %s DESC LIMIT %s", $dbfield, $dbrows);
+  $result = db_query_range("SELECT statistics.nid,node.title FROM statistics LEFT JOIN node ON statistics.nid = node.nid ORDER BY %s DESC LIMIT", $dbfield, 0, $dbrows);
   while ($nid = db_fetch_array($result)) {
     $content = node_load(array("nid" => $nid["nid"]));
     $links = link_node($content, 1);
diff --git a/modules/statistics/statistics.module b/modules/statistics/statistics.module
index afa850d7941f8bc2d54143be0b9f85121031bb7a..7100ad2ec6344475ed915d7001231e40f359bbc1 100644
--- a/modules/statistics/statistics.module
+++ b/modules/statistics/statistics.module
@@ -323,7 +323,7 @@ function statistics_admin() {
 
 /* Displays the various admin tables */
 function statistics_admin_count_table($dbfield, $dbrows) {
-  $result = db_query("SELECT statistics.nid, statistics.daycount, statistics.totalcount, statistics.timestamp, node.title FROM statistics LEFT JOIN node USING (nid) WHERE statistics.%s <> '0' ORDER BY statistics.%s DESC LIMIT %s", $dbfield, $dbfield, $dbrows);
+  $result = db_query_range("SELECT statistics.nid, statistics.daycount, statistics.totalcount, statistics.timestamp, node.title FROM statistics LEFT JOIN node USING (nid) WHERE statistics.%s <> '0' ORDER BY statistics.%s DESC", $dbfield, $dbfield, 0, $dbrows);
 
   $header = array(t("title"), t("today"), t("all time"), t("last hit"), t("operations"));
 
@@ -346,24 +346,24 @@ function statistics_admin_accesslog_table($type, $id) {
     /* retrieve user access logs */
     if ($id) {
       /* retrieve recent access logs for user $id */
-      $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE uid = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
+      $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE uid = '%s' ORDER BY timestamp DESC", $id, 0, $limit1);
     }
     else {
       /* retrieve recent access logs for all users */
-      $result = db_query("SELECT nid, url, hostname, uid, MAX(timestamp) AS timestamp FROM accesslog WHERE uid <> '0' GROUP BY uid, nid, url, hostname ORDER BY timestamp DESC LIMIT %s", $limit1);
+      $result = db_query_range("SELECT nid, url, hostname, uid, MAX(timestamp) AS timestamp FROM accesslog WHERE uid <> '0' GROUP BY uid, nid, url, hostname ORDER BY timestamp DESC", 0, $limit1);
     }
   }
   else if ($type == 2) {
     /* retrieve recent access logs for node $id */
-    $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE nid = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
+    $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE nid = '%s' ORDER BY timestamp DESC", $id, 0, $limit1);
   }
   else if ($type == 3) {
     /* retrieve recent access logs for hostname $id */
-    $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE hostname = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
+    $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE hostname = '%s' ORDER BY timestamp DESC %s", $id, 0, $limit1);
   }
   else {
     /* retrieve all recent access logs */
-    $result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog ORDER BY timestamp DESC LIMIT %s", $limit0);
+    $result = db_query_range("SELECT nid, url, hostname, uid, timestamp FROM accesslog ORDER BY timestamp DESC", 0, $limit0);
   }
 
   $header = array(t("timestamp"), t("title"), t("user"), t("hostname"), t("referrer"));
@@ -399,31 +399,31 @@ function statistics_recent_refer($nid = 0) {
 
   if ($nid > 0) {
     if ($view == "all") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url <> '' ORDER BY timestamp DESC";
     }
     elseif ($view == "internal") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC";
       $describe = "internal ";
     }
     else {
       /* default to external referrers */
-      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC";
       $describe = "external ";
     }
 
-    $result = db_query($query);
+    $result = db_query_range($query, 0, 15);
     $output = "<h3>". t("Most recent %describe referrers for node", array("%describe" => $describe)) ."\"". l($node->title, "node/view/$nid", array("title" => t("View this posting."))) ."\"</h3>";
   }
   else {
     if ($view == "all") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE url <> '' ORDER BY timestamp DESC";
     }
     elseif ($view == "internal") {
-      $query = "SELECT url,timestamp FROM accesslog WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC";
       $describe = "internal ";
     }
     else {
-      $query = "SELECT url,timestamp FROM accesslog WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
+      $query = "SELECT url,timestamp FROM accesslog WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC";
       $describe = "external ";
     }
 
@@ -792,7 +792,7 @@ function statistics_display_online_block() {
 /* Display linked title based on field name */
 function statistics_title_list($dbfield, $dbrows) {
   /* valid dbfields: totalcount, daycount, timestamp */
-  return db_query("SELECT statistics.nid, node.title, u.uid, u.name FROM statistics LEFT JOIN node ON statistics.nid = node.nid LEFT JOIN users u ON node.uid = u.uid WHERE %s <> '0' AND node.status = 1 ORDER BY %s DESC LIMIT %s", "statistics.". $dbfield, "statistics.". $dbfield, $dbrows);
+  return db_query_range("SELECT statistics.nid, node.title, u.uid, u.name FROM statistics LEFT JOIN node ON statistics.nid = node.nid LEFT JOIN users u ON node.uid = u.uid WHERE %s <> '0' AND node.status = 1 ORDER BY %s DESC", "statistics.". $dbfield, "statistics.". $dbfield, 0, $dbrows);
 }
 
 
@@ -890,7 +890,7 @@ function statistics_summary($dbfield, $dbrows) {
 
 
   $output = "";
-  $result = db_query("SELECT statistics.nid,node.title FROM statistics LEFT JOIN node ON statistics.nid = node.nid ORDER BY %s DESC LIMIT %s", $dbfield, $dbrows);
+  $result = db_query_range("SELECT statistics.nid,node.title FROM statistics LEFT JOIN node ON statistics.nid = node.nid ORDER BY %s DESC LIMIT", $dbfield, 0, $dbrows);
   while ($nid = db_fetch_array($result)) {
     $content = node_load(array("nid" => $nid["nid"]));
     $links = link_node($content, 1);
diff --git a/modules/taxonomy.module b/modules/taxonomy.module
index a6374b929bdf028da85f37ac2284e68f450e1a2c..2737844ba1c09cbd857a824de1598ec3e30191eb 100644
--- a/modules/taxonomy.module
+++ b/modules/taxonomy.module
@@ -268,7 +268,6 @@ function taxonomy_overview() {
   $vocabularies = taxonomy_get_vocabularies();
   foreach ($vocabularies as $vocabulary) {
     $links = array();
-
     $rows[] = array($vocabulary->name, array("data" => $vocabulary->types, "align" => "center"), l(t("edit vocabulary"), "admin/taxonomy/editvocabulary/$vocabulary->vid"), l(t("add term"), "admin/taxonomy/addterm/$vocabulary->vid"), l(t("preview form"), "admin/taxonomy/preview/vocabulary/$vocabulary->vid"));
 
     $tree = taxonomy_get_tree($vocabulary->vid);
@@ -644,7 +643,7 @@ function taxonomy_select_nodes($taxonomy, $pager = 1) {
   global $user;
 
   if ($taxonomy->operator == "or") {
-    $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.created, n.changed, n.uid, u.name FROM node n LEFT JOIN term_node r ON n.nid = r.nid LEFT JOIN users u ON n.uid = u.uid WHERE tid IN ($taxonomy->str_tids) AND n.status = '1' ORDER BY static DESC, created DESC";
+    $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.created, n.changed, n.uid, n.static, n.created, u.name FROM node n LEFT JOIN term_node r ON n.nid = r.nid LEFT JOIN users u ON n.uid = u.uid WHERE tid IN ($taxonomy->str_tids) AND n.status = '1' ORDER BY static DESC, created DESC";
     $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM node n LEFT JOIN term_node r ON n.nid = r.nid LEFT JOIN users u ON n.uid = u.uid WHERE tid IN ($taxonomy->str_tids) AND n.status = '1'";
   }
   else {
@@ -659,7 +658,7 @@ function taxonomy_select_nodes($taxonomy, $pager = 1) {
     $result = pager_query($sql, variable_get("default_nodes_main", 10) , 0, $sql_count);
   }
   else {
-    $result = db_query($sql ." LIMIT 15");
+    $result = db_query_range($sql, 0, 15);
   }
 
   return $result;
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index a6374b929bdf028da85f37ac2284e68f450e1a2c..2737844ba1c09cbd857a824de1598ec3e30191eb 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -268,7 +268,6 @@ function taxonomy_overview() {
   $vocabularies = taxonomy_get_vocabularies();
   foreach ($vocabularies as $vocabulary) {
     $links = array();
-
     $rows[] = array($vocabulary->name, array("data" => $vocabulary->types, "align" => "center"), l(t("edit vocabulary"), "admin/taxonomy/editvocabulary/$vocabulary->vid"), l(t("add term"), "admin/taxonomy/addterm/$vocabulary->vid"), l(t("preview form"), "admin/taxonomy/preview/vocabulary/$vocabulary->vid"));
 
     $tree = taxonomy_get_tree($vocabulary->vid);
@@ -644,7 +643,7 @@ function taxonomy_select_nodes($taxonomy, $pager = 1) {
   global $user;
 
   if ($taxonomy->operator == "or") {
-    $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.created, n.changed, n.uid, u.name FROM node n LEFT JOIN term_node r ON n.nid = r.nid LEFT JOIN users u ON n.uid = u.uid WHERE tid IN ($taxonomy->str_tids) AND n.status = '1' ORDER BY static DESC, created DESC";
+    $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.created, n.changed, n.uid, n.static, n.created, u.name FROM node n LEFT JOIN term_node r ON n.nid = r.nid LEFT JOIN users u ON n.uid = u.uid WHERE tid IN ($taxonomy->str_tids) AND n.status = '1' ORDER BY static DESC, created DESC";
     $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM node n LEFT JOIN term_node r ON n.nid = r.nid LEFT JOIN users u ON n.uid = u.uid WHERE tid IN ($taxonomy->str_tids) AND n.status = '1'";
   }
   else {
@@ -659,7 +658,7 @@ function taxonomy_select_nodes($taxonomy, $pager = 1) {
     $result = pager_query($sql, variable_get("default_nodes_main", 10) , 0, $sql_count);
   }
   else {
-    $result = db_query($sql ." LIMIT 15");
+    $result = db_query_range($sql, 0, 15);
   }
 
   return $result;
diff --git a/modules/user.module b/modules/user.module
index 631e01fa6393886a4e0979caf8f43b9d7215696b..5a0e533688f6a1d554b4e420ce8fbbb4915a0460 100644
--- a/modules/user.module
+++ b/modules/user.module
@@ -74,7 +74,7 @@ function user_load($array = array()) {
       $query .= "u.$key = '". check_query($value) ."' AND ";
     }
   }
-  $result = db_query("SELECT u.*, r.name AS role FROM users u LEFT JOIN role r ON u.rid = r.rid WHERE $query u.status < 3 LIMIT 1");
+  $result = db_query_range("SELECT u.*, r.name AS role FROM users u LEFT JOIN role r ON u.rid = r.rid WHERE $query u.status < 3", 0, 1);
 
   $user = db_fetch_object($result);
   if ($user->data && $data = unserialize($user->data)) {
@@ -299,7 +299,7 @@ function user_perm() {
 
 function user_search($keys) {
   global $PHP_SELF;
-  $result = db_query("SELECT * FROM users WHERE name LIKE '%$keys%' LIMIT 20");
+  $result = db_query_range("SELECT * FROM users WHERE name LIKE '%$keys%'", 0, 20);
   while ($account = db_fetch_object($result)) {
     $find[$i++] = array("title" => $account->name, "link" => (strstr($PHP_SELF, "admin") ? url("admin/user/edit/$account->uid") : url("user/view/$account->uid")), "user" => $account->name);
   }
@@ -375,7 +375,7 @@ function user_block($op = "list", $delta = 0) {
         break;
       case 2:
 
-        $result = db_query("SELECT uid, name FROM users WHERE status != '0' ORDER BY uid DESC LIMIT 5");
+        $result = db_query_range("SELECT uid, name FROM users WHERE status != '0' ORDER BY uid DESC", 0, 5);
         while ($account = db_fetch_object($result)) {
           $items[] = l((strlen($account->name) > 15 ? substr($account->name, 0, 15) . '...' : $account->name), "user/view/$account->uid");
         }
@@ -1257,7 +1257,6 @@ function user_roles($membersonly = 0) {
 }
 
 function user_admin_perm($edit = array()) {
-  global $tid;
 
   if ($edit) {
 
@@ -1265,16 +1264,14 @@ function user_admin_perm($edit = array()) {
     ** Save permissions:
     */
 
-    $tid = check_input($edit["tid"]);
-
     $result = db_query("SELECT * FROM role");
     while ($role = db_fetch_object($result)) {
       // delete, so if we clear every checkbox we reset that role;
       // otherwise permissions are active and denied everywhere
-      db_query("DELETE FROM permission WHERE rid = '%s' AND tid = '%s'", $role->rid, $tid);
+      db_query("DELETE FROM permission WHERE rid = '%s'", $role->rid);
       $perm = $edit[$role->rid] ? implode(", ", array_keys($edit[$role->rid])) : "";
       if ($perm) {
-        db_query("INSERT INTO permission (rid, perm, tid) VALUES ('%s', '%s', '%s')", $role->rid, $perm, $tid);
+        db_query("INSERT INTO permission (rid, perm) VALUES ('%s', '%s')", $role->rid, $perm);
       }
 
     }
@@ -1295,7 +1292,7 @@ function user_admin_perm($edit = array()) {
   ** Compile role array:
   */
 
-  $result = db_query("SELECT r.rid, p.perm FROM role r LEFT JOIN permission p ON r.rid = p.rid WHERE tid = '%s' ORDER BY name", $tid);
+  $result = db_query("SELECT r.rid, p.perm FROM role r LEFT JOIN permission p ON r.rid = p.rid ORDER BY name");
   $roles = array();
   while ($role = db_fetch_object($result)) {
     $role_perms[$role->rid] = $role->perm;
@@ -1323,7 +1320,6 @@ function user_admin_perm($edit = array()) {
   }
 
   $output = table($header, $rows);
-  $output .= form_hidden("tid", $tid);
   $output .= form_submit(t("Save permissions"));
 
   return form($output);
diff --git a/modules/user/user.module b/modules/user/user.module
index 631e01fa6393886a4e0979caf8f43b9d7215696b..5a0e533688f6a1d554b4e420ce8fbbb4915a0460 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -74,7 +74,7 @@ function user_load($array = array()) {
       $query .= "u.$key = '". check_query($value) ."' AND ";
     }
   }
-  $result = db_query("SELECT u.*, r.name AS role FROM users u LEFT JOIN role r ON u.rid = r.rid WHERE $query u.status < 3 LIMIT 1");
+  $result = db_query_range("SELECT u.*, r.name AS role FROM users u LEFT JOIN role r ON u.rid = r.rid WHERE $query u.status < 3", 0, 1);
 
   $user = db_fetch_object($result);
   if ($user->data && $data = unserialize($user->data)) {
@@ -299,7 +299,7 @@ function user_perm() {
 
 function user_search($keys) {
   global $PHP_SELF;
-  $result = db_query("SELECT * FROM users WHERE name LIKE '%$keys%' LIMIT 20");
+  $result = db_query_range("SELECT * FROM users WHERE name LIKE '%$keys%'", 0, 20);
   while ($account = db_fetch_object($result)) {
     $find[$i++] = array("title" => $account->name, "link" => (strstr($PHP_SELF, "admin") ? url("admin/user/edit/$account->uid") : url("user/view/$account->uid")), "user" => $account->name);
   }
@@ -375,7 +375,7 @@ function user_block($op = "list", $delta = 0) {
         break;
       case 2:
 
-        $result = db_query("SELECT uid, name FROM users WHERE status != '0' ORDER BY uid DESC LIMIT 5");
+        $result = db_query_range("SELECT uid, name FROM users WHERE status != '0' ORDER BY uid DESC", 0, 5);
         while ($account = db_fetch_object($result)) {
           $items[] = l((strlen($account->name) > 15 ? substr($account->name, 0, 15) . '...' : $account->name), "user/view/$account->uid");
         }
@@ -1257,7 +1257,6 @@ function user_roles($membersonly = 0) {
 }
 
 function user_admin_perm($edit = array()) {
-  global $tid;
 
   if ($edit) {
 
@@ -1265,16 +1264,14 @@ function user_admin_perm($edit = array()) {
     ** Save permissions:
     */
 
-    $tid = check_input($edit["tid"]);
-
     $result = db_query("SELECT * FROM role");
     while ($role = db_fetch_object($result)) {
       // delete, so if we clear every checkbox we reset that role;
       // otherwise permissions are active and denied everywhere
-      db_query("DELETE FROM permission WHERE rid = '%s' AND tid = '%s'", $role->rid, $tid);
+      db_query("DELETE FROM permission WHERE rid = '%s'", $role->rid);
       $perm = $edit[$role->rid] ? implode(", ", array_keys($edit[$role->rid])) : "";
       if ($perm) {
-        db_query("INSERT INTO permission (rid, perm, tid) VALUES ('%s', '%s', '%s')", $role->rid, $perm, $tid);
+        db_query("INSERT INTO permission (rid, perm) VALUES ('%s', '%s')", $role->rid, $perm);
       }
 
     }
@@ -1295,7 +1292,7 @@ function user_admin_perm($edit = array()) {
   ** Compile role array:
   */
 
-  $result = db_query("SELECT r.rid, p.perm FROM role r LEFT JOIN permission p ON r.rid = p.rid WHERE tid = '%s' ORDER BY name", $tid);
+  $result = db_query("SELECT r.rid, p.perm FROM role r LEFT JOIN permission p ON r.rid = p.rid ORDER BY name");
   $roles = array();
   while ($role = db_fetch_object($result)) {
     $role_perms[$role->rid] = $role->perm;
@@ -1323,7 +1320,6 @@ function user_admin_perm($edit = array()) {
   }
 
   $output = table($header, $rows);
-  $output .= form_hidden("tid", $tid);
   $output .= form_submit(t("Save permissions"));
 
   return form($output);