CodeIgniter Forums

Full Version: What's the difference?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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

El Forum

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

El Forum

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

El Forum

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

El Forum

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

thanks for replying.