CodeIgniter - database result array but only one row

10,379

Solution 1

Use the row() method instead:

$data = $this->db->from('view')->where('alias', $alias)->get()->row();

[Oh, you don't want to use objects. row_array() then. But consider objects.]

Solution 2

You can do this:

$data = $this->db->from('view')->where('alias', $alias)->get()->row_array();

This does the same as minitech's answer, only it returns an array (like you want) instead of an object.

Share:
10,379
Zar
Author by

Zar

Updated on June 08, 2022

Comments

  • Zar
    Zar almost 2 years

    I'm looking for a way to return an array from the database query, but only for the first row. I'd prefer not to use objects..

    My current solution:

    //Gather
    $data = $this->db->from('view')->where('alias', $alias)->get()->result_array();
    
    //Collect the first row only
    $data = $data[0];
    

    Which is pretty ugly to be honest. As said, I'd prefer to not use objects.