Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code igniter large query results result_array() result

I am looking at this.

I have queries that return up to 10,000 rows and usually fills up the memory

I am currently doing:

$this->db->get()->result_array();

Is there a way to not load all of the data into memory and use some sort of cursor? It seems result and result array are both arrays. (one is array of objects other is an array of arrays)

like image 244
Chris Muench Avatar asked Jan 06 '13 23:01

Chris Muench


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");

What is Row_array?

row_array() Returns a single result row. If your query has more than one row, it returns only the first row. Identical to the row() method, except it returns an array.

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

If you are using Active Record, which I would personally recommend, limit() should achieve what you are looking for.

$this->db->limit();

Can also use in this way, slightly easier and less lines of code:

$query = $this->db->get('Table_Name', 50); //syntax db->get('Table',limit_val);
return $query->result();

Also can return a limit with an offset:

$this->db->limit(10, 20); // Second parameter lets you set a result offset

Link for more help on Active Record Query's https://www.codeigniter.com/userguide2/database/active_record.html#select

like image 107
ityler22 Avatar answered Nov 03 '22 00:11

ityler22


Mysql offset is the pointer you are looking for. You can use it like:

    $this->db->get(tableName,10,20);

The second parameter and the third one help you to set limit and offset .

Here are more details

like image 30
mamdouh alramadan Avatar answered Nov 03 '22 00:11

mamdouh alramadan