![]() |
Difference between usage of return $query->result() and return $query-row() - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Difference between usage of return $query->result() and return $query-row() (/showthread.php?tid=51087) |
Difference between usage of return $query->result() and return $query-row() - El Forum - 04-20-2012 [eluser]Michal1[/eluser] Hello guys, when I want to get some data from database I am using Code: return $query->result() which works perfectly fine for me and I know that it returns array of objects. So then in controller I can do something like Code: $data['articles'] = $query; and then in view for example if I want to display "title" of all articles I do simply Code: <?php if (isset($articles)): foreach ($articles as $row) :?> And then there is second option, which is return $query->row() which I know returns only one row. So for example when I want to get only one article in model I use Code: return $query->row(); but now how to use in controller? I am trying Code: $article = $query; and then in view Code: <?php echo $article->title; ?> but it does not work so I suppose I dont understand it correctly. Btw of course I missed parts like $query = $this->db->get('something'); in the code here etc. I of course do it in my real code. Thank you Difference between usage of return $query->result() and return $query-row() - El Forum - 04-20-2012 [eluser]InsiteFX[/eluser] You still need to pass it to the view using. Code: $data['name'] = 'whatever'; Difference between usage of return $query->result() and return $query-row() - El Forum - 04-30-2012 [eluser]Stefan Hueg[/eluser] result() is working for single results, too, and you have nothing to change. To make the difference clearer: use result() if you expect to get 0-n results, whereas n can be any number, use row() if you exactly know that you can only get 0-1 results (by limiting the query using $this->db->limit(...) for example) or in case that you are accessing unique data where you are absolutely sure that there is just one result. |