summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/database/DB_query_builder.php16
-rw-r--r--user_guide_src/source/changelog.rst1
2 files changed, 16 insertions, 1 deletions
diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php
index e92c57091..c180a17e9 100644
--- a/system/database/DB_query_builder.php
+++ b/system/database/DB_query_builder.php
@@ -1379,7 +1379,16 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
$this->from($table);
}
- $result = ($this->qb_distinct === TRUE OR ! empty($this->qb_orderby))
+ // ORDER BY usage is often problematic here (most notably
+ // on Microsoft SQL Server) and ultimately unnecessary
+ // for selecting COUNT(*) ...
+ if ( ! empty($this->qb_orderby))
+ {
+ $orderby = $this->qb_orderby;
+ $this->qb_orderby = NULL;
+ }
+
+ $result = ($this->qb_distinct === TRUE)
? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
: $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
@@ -1387,6 +1396,11 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
{
$this->_reset_select();
}
+ // If we've previously reset the qb_orderby values, get them back
+ elseif ( ! isset($this->qb_orderby))
+ {
+ $this->qb_orderby = $orderby;
+ }
if ($result->num_rows() === 0)
{
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index d0d0a10eb..466726a7e 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -12,6 +12,7 @@ Bug fixes for 3.0.5
- Fixed a bug (#4391) - :doc:`Email Library <libraries/email>` method ``reply_to()`` didn't apply Q-encoding.
- Fixed a bug (#4384) - :doc:`Pagination Library <libraries/pagination>` ignored (possible) *cur_page* configuration value.
+- Fixed a bug (#4395) - :doc:`Query Builder <database/query_builder>` method ``count_all_results()`` still fails if an ``ORDER BY`` condition is used.
Version 3.0.4
=============