Welcome Guest, Not a member yet? Register   Sign In
Difference between usage of return $query->result() and return $query-row()
#1

[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) :?>
<?php echo $row->title; ?>
blablabla

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
#2

[eluser]InsiteFX[/eluser]
You still need to pass it to the view using.
Code:
$data['name'] = 'whatever';
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB