Cormac Hallinan
Cormac Hallinan

Reputation: 187

php export MySql to excel sheet wont work

The error message i am getting is with the headers. I am using mysqli_ i used mysql_ on a blank page to test it. When I put it into the click on my real page it told me I couldn't use mySql_ as it was old so converted it to mySqli_. the error message i'm getting now is:

"Warning: Cannot modify header information - headers already sent by (output started at /xx/xxx/xxx.php:183) in /xxx/xxx/xxx.php on line 232 Warning: Cannot modify header information - headers already sent by (output started at /xxx/xxx/xxx.php:183) in /xxx/xxx/xxx.php on line 233 Fatal error: Call to undefined function outputcsv() in /xxx/xxx/xxx.php on line 237"

require_once 'dbconfig.php';

$conn = new mysqli("xxxx", "xxxx", $password, $dbname);//host, user, password, database

$result = $conn->query('SELECT * FROM reports') or die(mysqli_error());

//these two lines are the lines 232 and 233
header('Content-Type: text/csv'); // tell the browser to treat file as CSV
header('Content-Disposition: attachment;filename=report.csv'); // tell browser to download a file in user's system with name export.csv

$row = mysqli_fetch_assoc($result); // Get the column names
if ($row) {
    outputcsv(array_keys($row)); // It wil pass column names to outputcsv function
}

//this line here is 237
while ($row) {
    outputcsv($row);    // loop is used to fetch all the rows from table and pass them to outputcsv func
    $row = mysqli_fetch_assoc($result);
 }

 function outputcsv($fields) {
   $csv = '';
    foreach ($fields as $field) {
        $csv .= '"' . $field . '",';
    }
    $csv .= "\r\n";     //Give a carriage return and new line space after each record
    echo $csv;
}

Upvotes: 0

Views: 119

Answers (2)

Rohit Kumar
Rohit Kumar

Reputation: 1958

You need to set output buffer on in config file

or

Just write ob_start() at first line of code ..

And i also suggest to export in xls format for better compatibilty .

Upvotes: 1

Patrick Murphy
Patrick Murphy

Reputation: 2329

You need to look up php output buffering.

You are echoing or sending some sort of data on line 183 headers already sent by (output started at /xx/xxx/xxx.php:183) and then modifying the headers. You can use OB to buffer the information you would send to the browser, modify the headers and then flush the buffer.

ob_start();
require_once 'dbconfig.php';

$conn = new mysqli("xxxx", "xxxx", $password, $dbname);//host, user, password, database

$result = $conn->query('SELECT * FROM reports') or die(mysqli_error());

//these two lines are the lines 232 and 233
header('Content-Type: text/csv'); // tell the browser to treat file as CSV
header('Content-Disposition: attachment;filename=report.csv'); // tell browser to download a file in user's system with name export.csv

$row = mysqli_fetch_assoc($result); // Get the column names
if ($row) {
    outputcsv(array_keys($row)); // It wil pass column names to outputcsv function
}

//this line here is 237
while ($row) {
    outputcsv($row);    // loop is used to fetch all the rows from table and pass them to outputcsv func
    $row = mysqli_fetch_assoc($result);
 }

 function outputcsv($fields) {
   $csv = '';
    foreach ($fields as $field) {
        $csv .= '"' . $field . '",';
    }
    $csv .= "\r\n";     //Give a carriage return and new line space after each record
    echo $csv;
}
ob_flush();

You can also use ob_get_flush() or ob_get_contents() to return the contents of the buffer to a string, allowing it to be returned from functions etc. Note: ob_get_contents() does not clear the buffer it just returns contents at that point.

I also see an error about outputcsv($fields) not being defined, but that might clear-up after the interpreter can get past the header issue.

Upvotes: 0

Related Questions