Part of the EllisLab Network

Bug Report

Orderby column RAND() error

Date: 07/31/2007 Severity: Minor
Status: Resolved Reporter: phildawson
Version: 1.5.4
Keywords: Libraries, Database Class

Description

The user guide for the orderby method in the Active Record Class explains that the options for the directions are asc or desc or RAND().

If RAND() is used for the second parameter it will produce an error, as it will create an invalid query ORDER BY column RAND()

The solution is to update the guide to use the following code for random ordering.

$this->db->orderby(‘RAND()’);

The orderby method in DB_active_rec.php line 408 should then have ‘RAND()’ removed from the array.

function orderby($orderby, $direction = ‘’)
  {
if (trim($direction) != '')
{
$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC', 'RAND()'), TRUE)) ? ' '.$direction : ' ASC';
}
     
      $this->ar_orderby[] = $orderby.$direction;
      return $this;
  }

Code Sample

$this->db->orderby('column', RAND());

Expected Result

Table ordered by random.

Actual Result

MySQL Error

Comment on Bug Report

Page 1 of 1 pages
Posted by: Derek Allard on 10 August 2007 6:24am
Derek Allard's avatar

Thanks for taking the time to report this.  It seems that every database has a different way to accomplish random order.  For example:

MySQL uses ORDER BY RAND()
PostgreSQL uses ORDER BY RANDOM()
Oracle uses a subquery with ORDER BY dbms_random.value
SQL Server users ORDER BY NEWID()

For now, we’ve removed mention of this from the user guide, but if you wanted to add the note on rand() for MySQL databases with the user contributed notes feature, that would be appreciated.

Posted by: barbazul on 27 October 2007 12:26pm
barbazul's avatar

Why not setting a common word for random across all drivers ie ci_random_ugly_name so when users call orderby(‘ci_random_ugly_name’); each driver would parse it its own way and you can still have a unified form of documenting it.


I have all copyrights on ‘ci_random_ugly_name’ so try to come up with a diferent name for it (:

Posted by: Derek Allard on 27 October 2007 12:36pm
Derek Allard's avatar

Yup, that is the long term solution.  Someone just needs to code it in, so for now I simply removed it.  If you code it up, I’ll push to use your name wink

Posted by: barbazul on 27 October 2007 2:59pm
barbazul's avatar

OK, I prepared a first attempt at fixing this based on information gathered from the internet. Testing in the different platforms is still required since I only have a mysql server installed.
Please, someone confirm this works on each supported platform or any problems (and preferrably their fixes) you might find

File system/database/DB_active_record.php
Line 406
+      //Check for random ordering
+      if (strtolower($orderby) == ‘random’)
+      {
+        $orderby = $this->_random();
+      }

File system/database/drivers/mssql/mssql_driver.php
Line 478
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “NEWID()”;
+  }

File system/database/drivers/mysql/mysql_driver.php
Line 485
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “RAND()”;
+  }

File system/database/drivers/mysqli/mysqli_driver.php
Line 475
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “RAND()”;
+  }

File system/database/drivers/oci8/oci8_driver.php
Line 601
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “dbms_random.value”;
+  }

File system/database/drivers/odbc/odbc_driver.php
Line 449
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “RND(”.time().”)”;
+  }

File system/database/drivers/postgre/postgre_driver.php
Line 482
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “RANDOM()”;
+  }

File system/database/drivers/sqlite/sqlite_driver.php
Line 481
+  //——————————————————————————————————
+ +  /**
+    * Order criteria used for random ordering
+    *
+    * @return string
+    */
+  function _random()
+  {
+      return “RANDOM()”;
+  }

Posted by: barbazul on 1 November 2007 10:12pm
barbazul's avatar

I’ve opened a forum thread to keep this bug alive as I personally don’t believe this bug to be really resolved

http://codeigniter.com/forums/viewthread/64145/

Posted by: Derek Allard on 18 December 2007 11:14am
Derek Allard's avatar

A note, ‘random’ has been added into order_by(), and replaces the old ‘RAND()’ keyword.

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?