CodeIgniter - return only one row?

Hailwood picture Hailwood · Nov 25, 2010 · Viewed 216.3k times · Source

At the moment if I am doing a query on the database that should only return one row, using:

...query stuff...
$query = $this->db->get();
$ret = $query->result();
return $ret[0]->campaign_id;

Is there a CodeIgniter function to return the first row? something like $query->row();

Or even better would be the ability to, if there was only one row, to just use the query object directly.

e.g. $query->campaign_id;

Answer

Alisson picture Alisson · Nov 25, 2010

You've just answered your own question :) You can do something like this:

$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;

You can read more about it here: http://www.codeigniter.com/user_guide/database/results.html