Welcome Guest, Not a member yet? Register   Sign In
DB Util csv_from_result() question
#1

[eluser]i_am_using_ci[/eluser]
Hi, I am trying to generate csv from result of my db select query...
My code (as in manual):

Code:
$this->load->dbutil();
            
$query = $this->db->query('select * from table where id = 1');
            
echo $this->dbutil->csv_from_result($query, ',', "\r\n");

I am getting error: "You must submit a valid result object"...

I am submitting valid result object as $query and my result set has about 40 rows...
I've tried to understand wtf and opened DB_utility.php CI class source, there are:

Code:
function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"') {
    if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
    {
        show_error('You must submit a valid result object');
    }

        ...

but my query result object doesn't have 'field_names' method...

What I am doing wrong?
#2

[eluser]Dam1an[/eluser]
To get the result object you need to do
Code:
$result = $query->result();
#3

[eluser]i_am_using_ci[/eluser]
Okay, but it's not like in manual firstly...
Secondly - I got result object, but error is same - there is no 'field_names' method...
#4

[eluser]Evil Wizard[/eluser]
the result object is an array of all your results. The csv_from_result() method extracts the field names from the result set to set the CSV header line and then iterates through the results. The method returns a string containing the result in csv format, this method does not write the csv file.
#5

[eluser]i_am_using_ci[/eluser]
And? I have result, but can't pass csv_from_result() method check (method_exists()):
Code:
if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
        {
            show_error('You must submit a valid result object');
        }
#6

[eluser]Evil Wizard[/eluser]
you could try a var_dump() of the result object, but php also has an fputcsv method that adds the line to the csv file and writes it, but it is (PHP 5 >= 5.1.0)
#7

[eluser]i_am_using_ci[/eluser]
I need to do it with CI, and understand why it doesn't work..
#8

[eluser]Evil Wizard[/eluser]
ok, just post the result of the var_dump($query->result()); and we'll try and help solve this with you ;o)
#9

[eluser]i_am_using_ci[/eluser]
array
0 =>
object(stdClass)[21]
public 'id' => string '1'
public 'data' => string 'aaa'
1 =>
object(stdClass)[22]
public 'id' => string '2'
public 'data' => string 'bbb'
#10

[eluser]Evil Wizard[/eluser]
is that the result from the db object via ...
Code:
$this->load->dbutil();
$query = $this->db->query('select * from table where id = 1');
$result = $query->result();
var_dump($result);
It doesn't seem like it as the db object result set is a class object that gets populated as an array of result objects, allowing the result objects to inherit methods and properties for manipulation




Theme © iAndrew 2016 - Forum software by © MyBB