Jump to content

PHP MySQL to CSV file stopped working


Recommended Posts

The code below worked perfectly on my old hosting service.   I've had to move to a new host and I get this error when I try to run the code.


Warning: Cannot modify header information - headers already sent by (output started at ../user/dbconfig.php:11)

This is the code:-

// connect to DATABASE!

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$options = [

$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset;port=$port";
try {
    // create a PDO connection with the configuration data
    $conn = new PDO($dsn, $user, $dbpassword, $options);
} catch (PDOException $e) {
    // report error message
    echo $e->getMessage();

    $ans = $_POST['answer'];
//Setup the filename that our CSV will have when it is downloaded.
if($ans == 'yes'){
    $fileName = 'answers-right.csv';
} elseif ($ans == 'no') {
    $fileName = 'answers-wrong.csv';

    // Create our SQL query.
    $sql = 'SELECT ID,email,firstname,lastname,message FROM checks WHERE answer = ?';
    $stmt = $conn->prepare($sql);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);      // NB: now using fetchAll because there's going to be more than one record

    //Get the column names.
$columnNames = array();
    //We only need to loop through the first row of our result
    //in order to collate the column names.
    $firstRow = $rows[0];
    foreach($firstRow as $colName => $val){
        $columnNames[] = $colName;
//If the file exists and is writeable then delete it
    //Delete the file
    $deleted = unlink($fileName);

//Set the Content-Type and Content-Disposition headers to force the download.
header('Content-Type: application/excel');
header('Content-Type: application/excel Content-Disposition: attachment; filename="' . $fileName . '"');

//Open up a file pointer
$fp = fopen('php://output', 'w');
//Start off by writing the column names to the file.
fputcsv($fp, $columnNames);
//Then, loop through the rows and write them to the CSV file.
foreach ($rows as $row) {
    fputcsv($fp, $row);
//Close the file pointer.

Would appreciate some help please?

Link to comment
Share on other sites

16 minutes ago, maxxd said:

../user/dbconfig.php outputs something - check that file.

Thanks for your message - appreciated!  ../user/dbconfig.php is just what it says.  It simply contains the following:-

// connect to DATABASE!
$host = 'localhost';
$user = 'username';
$dbpassword = 'mypassword';
$dbname = 'dbname';
$port = "3306";
$charset = 'utf8';

What's most confusing is that all worked on my previous web host's servers.   I've simply migrated the whole website so why is it now failing?

Link to comment
Share on other sites

I'll lay good money Barand is spot-on. Don't close out your php files - if there's a trailing newline it'll be regarded as output to the browser, and some editors add a newline by default on save.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.