Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem when using execute() after DB query
  • So if I call this
    $staff = DB::select()->from('staff')
     ->order_by('name_last', 'asc')
     ->order_by('name_first', 'asc')
     ->order_by('name_middle', 'asc')
     ->execute();
    print_r($staff);
    

    I get this...
    Fuel\Core\Database_Result_Cached Object
    (
        [_query:protected] => SELECT * FROM `staff` ORDER BY `name_last` ASC, `name_first` ASC, `name_middle` ASC
        [_result:protected] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [name_first] => Ftest
                        [name_middle] => Mtest
                        [name_last] => Ltest
                        [email] => email@example.com
                        [dob] => 1980-12-25
                        [address_line1] => Street Name
                        [address_line2] => Village Name
                        [address_line3] => TOWNSVILLE
                        [address_line4] => 
                        [address_postcode] => SW1A 1AA
    etc
                    )
    
            )
    
        [_total_rows:protected] => 1
        [_current_row:protected] => 0
        [_as_object:protected] => 
    )
    

    But if I execute afterwards, it doesn't work.
    $staff = DB::select()->from('staff')
     ->order_by('name_last', 'asc')
     ->order_by('name_first', 'asc')
     ->order_by('name_middle', 'asc');
    $staff->execute();
    print_r($staff);
    
    Fuel\Core\Database_Query_Builder_Select Object
    (
        [_select:protected] => Array
            (
            )
    
        [_distinct:protected] => 
        [_from:protected] => Array
            (
                [0] => staff
            )
    
        [_join:protected] => Array
            (
            )
    
        [_group_by:protected] => Array
            (
            )
    
        [_having:protected] => Array
            (
            )
    
        [_offset:protected] => 
        [_last_join:protected] => 
        [_where:protected] => Array
            (
            )
    
        [_order_by:protected] => Array
            (
                [0] => Array
                    (
                        [0] => name_last
                        [1] => asc
                    )
    
                [1] => Array
                    (
                        [0] => name_first
                        [1] => asc
                    )
    
                [2] => Array
                    (
                        [0] => name_middle
                        [1] => asc
                    )
    
            )
    
        [_limit:protected] => 
        [_type:protected] => 1
        [_lifetime:protected] => 
        [_sql:protected] => 1
        [_parameters:protected] => Array
            (
            )
    
        [_as_object:protected] => 
    )
    

    Why is that? I want to be able to put other queries in, such as wheres but in ifs, so hence the split.
  • Read it again:
    $staff = DB::select()->from('staff')
    ->order_by('name_last', 'asc')
    ->order_by('name_first', 'asc')
    ->order_by('name_middle', 'asc');
    $staff->execute();
    print_r($staff);

    $staff is still the query, not the result returned by the execution (which goes into oblivian as you execute it without assigning the result to a variable). This will work:
    $query = DB::select()->from('staff')
    ->order_by('name_last', 'asc')
    ->order_by('name_first', 'asc')
    ->order_by('name_middle', 'asc');
    $staff = $query->execute();
    print_r($staff);
  • Oh I see! I thought that $staff became the object that I would then execute. Thanks for your help, once again!

Howdy, Stranger!

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

In this Discussion