CodeIgniter - return only one row?

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();


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


$query->campaign_id;




8 Answers
8



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






lol, I was not actually expecting that to exist... awesome

– Hailwood
Nov 25 '10 at 20:10







Does this not waste out performance?

– Salitha Prasad
Aug 31 '16 at 7:56






$row = json_decode($query->row(), true); ended up using--> $row = $query->result_array()[0];

– CrandellWS
Sep 18 '18 at 3:34



$row = json_decode($query->row(), true);


$row = $query->result_array()[0];



This is better way as it gives you result in a single line:


$this->db->query("Your query")->row()->campaign_id;






Easy way to query on db.

– bkac
Oct 10 '16 at 7:00






not if there is no result

– kitaps
May 2 '18 at 6:35






That's the only simple answer. Thanks.

– Well Smith
Sep 1 '18 at 5:06




To add on to what Alisson said you could check to see if a row is returned.


// Query stuff ...
$query = $this->db->get();

if ($query->num_rows() > 0)

$row = $query->row();
return $row->campaign_id;


return null; // or whatever value you want to return for no rows found



To make the code clear that you are intending to get the first row, CodeIgniter now allows you to use:


if ($query->num_rows() > 0)
return $query->first_row();



To retrieve the first row.


$this->db->get()->row()->campaign_id;



Change only in two line and you are getting actually what you want.


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



try it.



We can get a single using limit in query




$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);


$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);



class receipt_model extends CI_Model


public function index()

$this->db->select('*');

$this->db->from('donor_details');

$this->db->order_by('donor_id','desc');

$query=$this->db->get();

$row=$query->row();

return $row;





Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế