Welcome Guest, Not a member yet? Register   Sign In
How do I not use foreach for a database result?
#1

[eluser]Tyler Diaz[/eluser]
Well I've been with code igniter for quite a while now and never wondered this until I tried it.

If I send a query to the DB that is expect only to return one result, a foreach() sounds unnecesary and bloating. How do I get that one single result, or other data information in the same row and parse it without having to call a foreach?

Thanks in advance, Tyler.
#2

[eluser]Wuushu[/eluser]
I usually do..

Code:
$items = $this->model_x->get_last();

if(isset($items[0]))
{
      echo $items[0]['field']; // or whatever you want to do with your ONE record
}
#3

[eluser]Tyler Diaz[/eluser]
[quote author="Wuushu" date="1248485542"]I usually do..

Code:
$items = $this->model_x->get_last();

if(isset($items[0]))
{
      echo $items[0]['field']; // or whatever you want to do with your ONE record
}
[/quote]

So you utilize a model to do the query, and the controller to parse it out?

For the Model, do you utilize Active record?
#4

[eluser]Chad Fulton[/eluser]
If you're using active record, you can do something like this:

Code:
$result = $this->db->where('uid', 1)
                   ->limit(1)
                   ->get('users');

$row = $result->row();

echo $row->uid;       // 1
echo $row->first_name // John
echo $row->last_name  // Smith

That will give you the single row you wanted. Does this give you the information you wanted?
#5

[eluser]Tyler Diaz[/eluser]
Thanks a ton Chad. Its a substitute to what I wanted but it does the job. (:

To anyone else who might have this doubt. Can you retrieve that information the same way using $this->db->query() ?
#6

[eluser]Wuushu[/eluser]
Yes, I use the model, sending back a result to the controller, which I do any if necessary logic onto, then I can loop over a result-set in a view, but that about as much logic as I do in a view. Ideally.

I rarely if ever write queries directly in the controller, and yes I use AR for most queries and "normal" queries where AR isn't sufficient enough for various reasons..
#7

[eluser]Chad Fulton[/eluser]
[quote author="Tyler Diaz" date="1248486141"]Thanks a ton Chad. Its a substitute to what I wanted but it does the job. (:

To anyone else who might have this doubt. Can you retrieve that information the same way using $this->db->query() ?[/quote]

You can use it exactly the same way without active record, i.e.:
Code:
$result = $this->db->query('SELECT * FROM users WHERE uid = 1 LIMIT 1');
$row = $result->row();

echo $row->uid; // 1

etc.
#8

[eluser]Tyler Diaz[/eluser]
[quote author="Chad Fulton" date="1248487929"][quote author="Tyler Diaz" date="1248486141"]Thanks a ton Chad. Its a substitute to what I wanted but it does the job. (:

To anyone else who might have this doubt. Can you retrieve that information the same way using $this->db->query() ?[/quote]

You can use it exactly the same way without active record, i.e.:
Code:
$result = $this->db->query('SELECT * FROM users WHERE uid = 1 LIMIT 1');
$row = $result->row();

echo $row->uid; // 1

etc.[/quote]

Thanks a ton. Hopefully this will help other that might have been wondering this same conflict.
#9

[eluser]garymardell[/eluser]
Code:
// some query stuffs
$query = $this->db->get();

$row = $query->row();

echo $row->field_name;

I think that's considered standard.




Theme © iAndrew 2016 - Forum software by © MyBB