Welcome Guest, Not a member yet? Register   Sign In
Fetching MySQL results without a loop?
#1

[eluser]Been Told[/eluser]
Hi everyone,
I'm a CI noob and I'm writing my first CI based script. I've run into a problem that seems stupid and so I thought I should take this opportunity to look stupid. Big Grin

The situation is this:
I have written a query in a controller, like this:
Code:
$data['query2'] = $this->db->get_where('resources', array('id' => $this->uri->segment(3)),1);
Then I load the view like this:
Code:
$this->load->view('details_view',$data);
Now, I know that the resulting query will only return 1 result. So, how do I use active records to fetch the result from this query?
Because the result() function page in the user guide here only features usage of result() inside loops - and I'm not sure how to "translate" that into just fetching one row. Without going through loops, as there will only be one result.

I hope I'm making sense here... Anyway, help is greatly appreciated.
I am running the latest CI (1.7.2) on XAMPP on Ubuntu, with PHP 5.2.
#2

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...sults.html, 3rd heading down

Code:
$row = $query2->row();
#3

[eluser]Been Told[/eluser]
[quote author="noctrum" date="1282322844"]http://ellislab.com/codeigniter/user-gui...sults.html, 3rd heading down

Code:
$row = $query2->row();
[/quote]
D'oh!
Thanks!
In the meantime I've figured to do this:
Code:
$result2 = $query2->result();
$output = $result2[0]->name;
But with user guide's solution, it looks a lot better:
Code:
$row = $query2->row();
$out = $row->name;

Thanks! Smile




Theme © iAndrew 2016 - Forum software by © MyBB