CodeIgniter Forums
What's the difference? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: What's the difference? (/thread-38980.html)



What's the difference? - El Forum - 02-25-2011

[eluser]gypmaster[/eluser]
Hi,

Been away from CI for a good while now (never really got too far into it previously) and just trying to get back into it now. Got a question about database functions ... what's the difference between:

Code:
function get_records()
    {
        $query = $this->db->get('data');
        $q=$query->result();
        $query->free_result();
        return $q;    
    }

and ...

Code:
function get_records()
    {
        $Q = $this->db->get('data');
        if ($Q->num_rows() > 0)
            {
                foreach ($Q->result() as $row)
                    {
                        $data[] = $row;
                    }
            }
        $Q->free_result();
        return $data;        
    }



What's the difference? - El Forum - 02-25-2011

[eluser]John_Betong[/eluser]
Your second function uses $data[] which has not been previously declared.

For both function I would use print_r(...) jus before the return statement to view the results.
 
 
 


What's the difference? - El Forum - 02-25-2011

[eluser]gypmaster[/eluser]
Thanks John ...

... I meant what's the difference operationally?

Why would I want to loop through the $q->result and build an array from it?

Isn't that slower?


What's the difference? - El Forum - 02-25-2011

[eluser]John_Betong[/eluser]
[quote author="gypmaster" date="1298671429"]Thanks John ...

... I meant what's the difference operationally?

Why would I want to loop through the $q->result and build an array from it?

Isn't that slower?[/quote]

The loop splits the array then iterates which will add additional unnecessary processing.

I use it to create a formatted block by adding line breaks, bold, tabbing, etc which I know is very naughty as far as MVC is concerned since formatting should be done in the view. It makes it easy to echo the $result in a view so much easier.
 
 
 


What's the difference? - El Forum - 02-25-2011

[eluser]gypmaster[/eluser]
ok, I get it now.

thanks for replying.