Welcome Guest, Not a member yet? Register   Sign In
What's the difference?
#1

[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;        
    }
#2

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

[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?
#4

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

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

thanks for replying.




Theme © iAndrew 2016 - Forum software by © MyBB