Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Silly DB question
  • How do you guys normally select a single row? Here is what I've got, but it seems a little weird:
    $result = \DB::select()->from('faq')->where('faq_id', $faq_id)->execute()->as_array();
    return isset($result[0]) ? $result[0] : false;
    
  • DB methods always return an array, regardless of the result. This means you can depend on the result, and don't have to test first. You disable that feature by returning different data types based on the result, which means that in your controller code you now have to check types before you use the data. It is better to do
    // in your method
    return \DB::select()->from('faq')->where('faq_id', $faq_id)->execute()->as_array();
    
    // in your controller (random class and method name)
    $result = Class::getrow();
    
    if ($result)
    {
        // there was a result! If not, an empty array evaluates to false.
    }
    
  • Brad Proctor wrote on Thursday 26th of January 2012:
    How do you guys normally select a single row? Here is what I've got, but it seems a little weird:
    $result = \DB::select()->from('faq')->where('faq_id', $faq_id)->execute()->as_array();
    return isset($result[0]) ? $result[0] : false;
    

    If you know for sure the result will be 1 row (selecting on primary key or using LIMIT 1 for example) you can use current() instead of as_array():
    $data = \DB::select()->from('table')->where('id', 1)->execute()->current();
    
  • Awesome, thanks guys!

Howdy, Stranger!

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

In this Discussion