How do I not use foreach for a database result? - 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: How do I not use foreach for a database result? (/showthread.php?tid=20897) |
How do I not use foreach for a database result? - El Forum - 07-24-2009 [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. How do I not use foreach for a database result? - El Forum - 07-24-2009 [eluser]Wuushu[/eluser] I usually do.. Code: $items = $this->model_x->get_last(); How do I not use foreach for a database result? - El Forum - 07-24-2009 [eluser]Tyler Diaz[/eluser] [quote author="Wuushu" date="1248485542"]I usually do.. Code: $items = $this->model_x->get_last(); So you utilize a model to do the query, and the controller to parse it out? For the Model, do you utilize Active record? How do I not use foreach for a database result? - El Forum - 07-24-2009 [eluser]Chad Fulton[/eluser] If you're using active record, you can do something like this: Code: $result = $this->db->where('uid', 1) That will give you the single row you wanted. Does this give you the information you wanted? How do I not use foreach for a database result? - El Forum - 07-24-2009 [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() ? How do I not use foreach for a database result? - El Forum - 07-24-2009 [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.. How do I not use foreach for a database result? - El Forum - 07-24-2009 [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'); etc. How do I not use foreach for a database result? - El Forum - 07-24-2009 [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'); etc.[/quote] Thanks a ton. Hopefully this will help other that might have been wondering this same conflict. How do I not use foreach for a database result? - El Forum - 07-24-2009 [eluser]garymardell[/eluser] Code: // some query stuffs I think that's considered standard. |