Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter, result() vs. result_array()

I use both result() and result_array().

Usually i like to get my result as array thats why i use result_array() mostly..

But i want to know which is the better approach that i should follow, Which one of them is more efficient to use in regards to performance?

Here is the Example i am talking about in codeigniter queries

$query = $this->db->get(); $result = $query->result_array(); 

or is this should be the better approach??

$query = $this->db->get(); $result = $query->result(); 

also right now i am using result_array in my generic model.

like image 229
Sizzling Code Avatar asked May 12 '13 11:05

Sizzling Code


People also ask

What is Result_array () in codeigniter?

result_array()This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this: $query = $this->db->query("YOUR QUERY");

How do you get rows in CI?

If you require to get only one record from database table using codeigniter query then you can do it using row(). we can easily return one row from database in codeigniter. I will give you simple example of fetch single record from database using mysql codeigniter.

What is active record codeigniter?

Active Record Class CodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.

How do I print a query in CI?

Use the below query to display the query string: print_r($this->db->last_query()); To display the query result follow this: print_r($query);


2 Answers

Result has an optional $type parameter which decides what type of result is returned. By default ($type = "object"), it returns an object (result_object()). It can be set to "array", then it will return an array of result, that being equivalent of caling result_array(). The third version accepts a custom class to use as a result object.

The code from CodeIgniter:

/** * Query result. Acts as a wrapper function for the following functions. * * @param string $type 'object', 'array' or a custom class name * @return array */ public function result($type = 'object') {     if ($type === 'array')     {         return $this->result_array();     }     elseif ($type === 'object')     {         return $this->result_object();     }     else     {         return $this->custom_result_object($type);     } } 

Arrays are technically faster, but they are not objects. It depends where do you want to use the result. Most of the time, arrays are sufficient.

like image 130
Whisperity Avatar answered Sep 29 '22 02:09

Whisperity


for the sake of reference:

// $query->result_object() === $query->result() // returns: Array ( [0] => stdClass Object ( [col_A] => val_1A , [col_B] => val_1B , ... )          [0] => stdClass Object ( [col_A] => val_2A , [col_B] => val_2B , ... )          ...         )   // $query->result_array() !== $query->result() // returns: Array ( [0] => Array ( [col_A] => val_1A , [col_B] => val_1B , ... )          [1] => Array ( [col_A] => val_2A , [col_B] => val_2B , ... )         ...        )  

codeigniter docs for result(), and result_array()

like image 21
dsdsdsdsd Avatar answered Sep 29 '22 04:09

dsdsdsdsd