Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Syntax for database order_by with multiple columns
  • I get database errors when I try to do an order_by by more than one column
    This works for one column:
     $query = DB::select(
       'auth_users.first_name',
       'auth_users.last_name',
       ->from('auth_users')
       order_by('last_name');
       ;
    
      $result = $query->execute();
      $data = $result->as_array();
    

    I've tried replacing the above order_by with the following and all give errors:
    ->order_by('last_name', 'first_name');
    ->order_by('last_name, first_name');
    ->order_by(array('last_name', 'first_name'));
    
  • Mitchell Steinberg wrote on Tuesday 24th of May 2011:
    I get database errors when I try to do an order_by by more than one column
    This works for one column:
     $query = DB::select(
       'auth_users.first_name',
       'auth_users.last_name',
       ->from('auth_users')
       order_by('last_name');
       ;
    
      $result = $query->execute();
      $data = $result->as_array();
    

    I've tried replacing the above order_by with the following and all give errors:
    ->order_by('last_name', 'first_name');
    ->order_by('last_name, first_name');
    ->order_by(array('last_name', 'first_name'));
    

    hey,
    I think you scenario is a bit incorrect, the correct way woud be:
    $result = DB::select('first_name', 'last_name')->from('auth_users')->order_by('first_name', 'asc')->order_by('last_name', 'asc')->as_object()->execute();
    and then:
    foreach ($result as ... )
    
  • Also:
    I've tried replacing the above order_by with the following and all give errors:
    Then tell us the error, no way for us to help you if you don't let us know what's wrong. Copy past the exact text of the error message and don't interpret.
  • Sorry for not including the error, but I assumed I was just missing the correct syntax for multiple order by fields which Huglester provided.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion