Biomehanika
Biomehanika

Reputation: 1540

Enabling PHP errors when creating a CSV file from database

This method for exporting data on csv has worked previously on other projects, but I can not make this work on here, and I am not sure about how to enable erros for this case.

This PHP file creates a comma-separated file containing an initial row with a single tab ("ID"), and then it should be creating a row for each match on the SELECT query from DB

<?php
session_start();
ob_start();

include('conexionbbdd.php');

    $file='informes/expositores_'.time().'.xls';

    header("Content-Type: application/xls");    
    header("Content-Disposition: attachment; filename=$file");  
    header("Pragma: no-cache"); 
    header("Expires: 0");        

    $output = fopen($file, 'w');

    fwrite($output, chr(239) . chr(187) . chr(191)); 

    fputcsv($output, array('ID'), "\t");    

    // fetch the data
    $rows1 = mysqli_query($con, "SELECT ex_id FROM expositores WHERE ex_id = '26'");

    // loop over the rows, outputting them
    while ($row1 = mysqli_fetch_assoc($rows1)) {     
        fputcsv($output, $row1, "\t");        
    }

    fclose($output);
    echo $file;


ob_end_flush();   
?>

In this particular case I've simplified this to maximu so, apart from the initial row, a unique row containg the "26" should be created (I've tested that the query works with PhpMyAdmin, there's an ID 26). But it does not.

It only creates correctly first row from this first fputcsv method:

fputcsv($output, array('ID'), "\t");  

No other row seems to be fetched or placed on the CSV.

As the entire PHP file's aim is to create the CSV file, no error is shown because it does not open on a new window.

Output:

enter image description here

Upvotes: 3

Views: 502

Answers (1)

Jay Blanchard
Jay Blanchard

Reputation: 34426

In order to solve this you will need to be able to view the errors. You can have a look in your error logs or add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1);

Upvotes: 2

Related Questions