Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter - return only one row?

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;

like image 723
Hailwood Avatar asked Nov 25 '10 19:11

Hailwood


People also ask

How to return single row in CodeIgniter?

$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.


1 Answers

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

like image 159
Alisson Avatar answered Sep 19 '22 07:09

Alisson