Q: Undefined id in query CodeIgniter I have a model in my application:  
public function get_news() 
{
    ....
        $this->load->database();


        $top_news = $this->db->query("SELECT * FROM News ");

        if ($top_news->num_rows()) 
        {
            $top_news = $top_news->result();
            $top_news['AuthorInfo'] = $this->get_author($top_news['Userid']);
            $this->cache->save('home.top_news', $top_news, 600);
        } 
        else 
        {
            $this->cache->save('home.top_news', NULL, 600);
        }
    }

    return $top_news;
}
public function get_author($author_id)
{
    $this->load->database();
    $author = $this->db->query("SELECT * FROM AdminUsers WHERE id=? LIMIT 1", array($author_id));
    if ($author->num_rows())
    {
        $author = $author->row_array(); 
    }
    else 
    {
        $author = NULL;
    }

    return $author;
}

I got the error:   
Message: Undefined index: Userid  

But this field exists in the table News in my database.
I don't understand where is my problem.
Help me please guys 
I wrote var_dump and I got
Array
(
  [0]=>stdClass object
    (
       [id]=>56
       [Userid]=>4
       ...


A: With this query you will get a result set (0,1 or more rows), and not one row:
    $top_news = $this->db->query("SELECT * FROM News ");

You need to loop through the results.
foreach ($top_news->result_array() as $row)
{
   $row['AuthorInfo'] = $this->get_author($row['Userid']);
   $this->cache->save('home.top_news', $row, 600);
}

If you're sure that you will receive only one row, or want to select the first row only you can use this:
 $row = $top_news->row_array(); 
 $row['AuthorInfo'] = $this->get_author($row['Userid']);
 $this->cache->save('home.top_news', $row, 600);


A: $top_news is an object, not an array, and if you want to get the first user id then you need to get the user id located in $top_news[0].
So change 
$this->get_author($top_news['Userid']); 
to 
$this->get_author($top_news[0]->Userid);

A: You should first get all news right and loop to set AuthorInfo and save cache result
Your query should be like following:
$query = $this->db->query("SELECT * FROM News");

$top_news = $query->result_array();

foreach($top_news as &$row)
{
     $row['AuthorInfo'] = $this->get_author($row['Userid']);
}

$this->cache->save('home.top_news', $top_news, 600);

