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)
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");
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.
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);
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With