• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter 3.1.7 unbuffered_row() returning NULL but row() works

#1
I am attempting to output a CSV from a query result. dbutil->csv_from_result() should work, but it only returns the column headers. No data is included.

I tracked it down to system/database/DB_utility.php which uses unbuffered_row().

Any calls to unbuffered_row() return `NULL`. If I change it to row(), I get a result. It makes no difference if I use unbuffered_row('array') or unbuffered_row('object')`. In fact, row('array') doesn't seem to work either.

Why does row() work but unbuffered_row() does not?

What am I missing?

EDIT: Okay, the problem seems to be that I called $query->result() prior to calling dbutil->csv_from_result().

New question: Is there a way to display the results of a query and simultaneously save the CSV of results without having to run the query twice?
Reply

#2
Has anyone seen this?
Reply

#3
@pbarney,

Answer to you new question...You could simple dump the results into an array or session variable or ... which you could use as needed.
Reply

#4
(04-24-2018, 06:46 AM)pbarney Wrote: Has anyone seen this?

The thread has been viewed ~130 times (this is shown in the topics list). Most wouldn't reply because they don't know the answer, as it should be - noone benefits from "I don't know" replies. I didn't reply because (pre-edit; and I just now see the added question at the end) how unbuffered_row() works is already explained in the documentation, and it seemed like you're on the right path to finding it out yourself anyway.

When you want to add more info/ask more questions, just reply again.

(03-10-2018, 11:22 AM)pbarney Wrote: New question: Is there a way to display the results of a query and simultaneously save the CSV of results without having to run the query twice?

It might be possible (I'm not entirely sure, and if it is, it would depend on the DB driver in use), but the real problem is that there's no reason why you or anyone else should ever need it to be.

You shouldn't slow down responses by doing archiving work at the same time, for the same reason why you shouldn't send emails from the web process, do database backups, etc. It might look like an easy way to do it, but it always causes problems down the line - large datasets will take lots of time to process (and might exhaust the memory limit, which is why unbuffered_row() is used here), networking issues can cause hangs, users can stop execution in the middle ...

Display what you need to display and be done with it. If for some reason, you need to archive something - make cronjobs do it.
Reply

#5
(04-24-2018, 07:10 AM)Narf Wrote:
(03-10-2018, 11:22 AM)pbarney Wrote: Is there a way to display the results of a query and simultaneously save the CSV of results without having to run the query twice?

It might be possible (I'm not entirely sure, and if it is, it would depend on the DB driver in use), but the real problem is that there's no reason why you or anyone else should ever need it to be.

Display what you need to display and be done with it. If for some reason, you need to archive something - make cronjobs do it.

Thanks for the reply. The use case is different from what you might expect. A query of many thousands of records that the end user needs to both see and download. I obviously can't store that much data in memory, so I was hoping for a dual solution.

After looking in-depth at the DB driver, it's clear to me that it's just not attainable. It's very much one-or-the-other.

My preferred solution would be to re-run the query when the use clicks the "download csv" button, but they end up clicking it about 95% of the time anyway. As a result, the client prefers a good user interface experience over performance.
Reply

#6
(04-24-2018, 06:55 AM)php_rocs Wrote: You could simple dump the results into an array or session variable or ... which you could use as needed.

Thanks for the response, php_rocs, but the dataset is far too large to store in memory or the session.
Reply

#7
(04-24-2018, 12:40 PM)pbarney Wrote:
(04-24-2018, 07:10 AM)Narf Wrote:
(03-10-2018, 11:22 AM)pbarney Wrote: Is there a way to display the results of a query and simultaneously save the CSV of results without having to run the query twice?

It might be possible (I'm not entirely sure, and if it is, it would depend on the DB driver in use), but the real problem is that there's no reason why you or anyone else should ever need it to be.

Display what you need to display and be done with it. If for some reason, you need to archive something - make cronjobs do it.

Thanks for the reply. The use case is different from what you might expect. A query of many thousands of records that the end user needs to both see and download. I obviously can't store that much data in memory, so I was hoping for a dual solution.

After looking in-depth at the DB driver, it's clear to me that it's just not attainable. It's very much one-or-the-other.

My preferred solution would be to re-run the query when the use clicks the "download csv" button, but they end up clicking it about 95% of the time anyway. As a result, the client prefers a good user interface experience over performance.

Well, that's the exact use case for which you don't need to both display and process into CSV - those happen on 2 separate requests anyway, so why would you need to do both at the same time?
Reply

#8
@pbarney,

I thought about your 2nd question some more and I thought of this solution. Datatables ( https://datatables.net/ ). I use it a lot. It is feature rich and FREE. It has a download feature that may meet your needs (that's assuming that you can use it).
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.